Issue serving static files in spray

168 Views Asked by At

I'm having some problems serving static files from a route that at his core look like this using spray 1.3.2:

ctx.withHttpResponseHeadersMapped(hm => hm ++ List(`Content-Disposition`("attachment",
   Map(("filename", attachment.name),
   `Content-Length`(attachment.bytes.length)))
   .withHttpResponseEntityMapped { e =>
     HttpEntity(new ContentType(
       MediaTypes.forExtension(attachment.name.split('.').last.toLowerCase).getOrElse(MediaTypes.`application/octet-stream`), None), e.data)  //also tried with just MediaTypes.`application/octet-stream` as default
   }
   .complete(attachment.bytes)

where attachment.name is just a string and attachment.bytes an Array[Byte].

In this way I'm able to download the file represented by the Array[Byte] from the browser interface, the problem is that when the file is in excel formats (xls, xlsx) the downloaded file seem to get corrupted during the process and wont open.
The strange thing is that for those corrupted files the dimension of the downloaded copy is bigger then the original and that in the browser console is possible to notice that the transfer-encoding is set to chunked with the Content-Length header removed.
Removing the specific media type has no effect.

I can't find a solution to make those excel files working smooth with this piece of code.

I don't understand where the transfer-encoding is set to chunked , file size seems not to be the cause because it's possible to serve big file without this issue for other formats (pdf, txt, jpeg, ...)

If someone can give an hint on what is happening I would appreciate it, thanks.

0

There are 0 best solutions below