You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a controller method for downloading a file (in my case a PDF). I based this on the assets controller code as mentioned in the documentation.
Originally I was trying to set the content type through the addHeader method, but it wasn't working correctly. In debugging I found you have to use the contentType method to set that header.
The Content-Type gets set to application/octet-stream instead of application/pdf. The .addHeader("Content-Type", MimeTypes.getDefaultMimeByExtension(filename))
is either ignored or just overwritten.
Behavior I saw was this. I was trying to design this controller method to display the PDF in the browser. But with the non-working code, the file downloaded instead. (Was still a perfectly usable file but the user had to go to their downloads folder and manually open it). Using the .contentType()
method things worked correctly and the PDF opened in the browser window.
So not sure if this is a bug or by design but maybe undocumented. But I spent a good bit of time troubleshooting the issue. Not sure if a check to not overwrite content-type if it's been manually set is a good fix, or just documenting this behavior somewhere.
The text was updated successfully, but these errors were encountered:
NickHirras
changed the title
Must call .contentType() for asset downloads
Must call .contentType() for downloads from controller
Dec 14, 2019
I have a controller method for downloading a file (in my case a PDF). I based this on the assets controller code as mentioned in the documentation.
Originally I was trying to set the content type through the addHeader method, but it wasn't working correctly. In debugging I found you have to use the contentType method to set that header.
Original code I was trying:
Working code:
It looks like if I set the Content-Type header through addHeader, the value was getting overwritten with some default value.
In the non-working code, I get these response headers:
In my working example, the response headers look like this for a test PDF file.
The Content-Type gets set to application/octet-stream instead of application/pdf. The
.addHeader("Content-Type", MimeTypes.getDefaultMimeByExtension(filename))
is either ignored or just overwritten.
Behavior I saw was this. I was trying to design this controller method to display the PDF in the browser. But with the non-working code, the file downloaded instead. (Was still a perfectly usable file but the user had to go to their downloads folder and manually open it). Using the
.contentType()
method things worked correctly and the PDF opened in the browser window.
So not sure if this is a bug or by design but maybe undocumented. But I spent a good bit of time troubleshooting the issue. Not sure if a check to not overwrite content-type if it's been manually set is a good fix, or just documenting this behavior somewhere.
The text was updated successfully, but these errors were encountered: