Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Must call .contentType() for downloads from controller #678

Open
NickHirras opened this issue Dec 14, 2019 · 0 comments
Open

Must call .contentType() for downloads from controller #678

NickHirras opened this issue Dec 14, 2019 · 0 comments

Comments

@NickHirras
Copy link

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:

    Result downloadByteArray(Context context, int filesizeBytes, String filename, byte[] data) throws IOException {
        return Results
            .ok()
            .addHeader("Content-Type", MimeTypes.getDefaultMimeByExtension(filename))
            .addHeader("Content-Length", Integer.toString(filesizeBytes))
            .addHeader("Content-Disposition", "inline; filename=\"" + filename + "\"")
            .renderRaw(data);
    }

Working code:

    Result downloadByteArray(Context context, int filesizeBytes, String filename, byte[] data) throws IOException {
        return Results
            .ok()
            .contentType(MimeTypes.getDefaultMimeByExtension(filename))
            .addHeader("Content-Length", Integer.toString(filesizeBytes))
            .addHeader("Content-Disposition", "inline; filename=\"" + filename + "\"")
            .renderRaw(data);
    }

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:

HTTP/1.1 200 OK
Date: Sat, 14 Dec 2019 01:47:07 GMT
Set-Cookie: SESSION=c5775...
Content-Disposition: inline; filename="test.pdf"
Content-Type: application/octet-stream;charset=utf-8
Content-Length: 588652
Server: Jetty(9.4.18.v20190429)

In my working example, the response headers look like this for a test PDF file.

HTTP/1.1 200 OK
Date: Sat, 14 Dec 2019 01:44:57 GMT
Set-Cookie: SESSION=fca5...
Content-Disposition: inline; filename="test.pdf"
Content-Type: application/pdf;charset=utf-8
Content-Length: 588652
Server: Jetty(9.4.18.v20190429)

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.

@NickHirras NickHirras changed the title Must call .contentType() for asset downloads Must call .contentType() for downloads from controller Dec 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant