152

I am returning a stream in some response setting the appropriate content-type header. The behavior I'm looking for is this:

  • If the browser is able to render content of the given content type then it should display it in the browser window.

  • If the browser doesn't know how to render the content, then it should display the "save as" dialog where the filename should be the one provided in the response.

The problem is that if I set the Content-Disposition header with:

"attachment; filename="myfile.txt""

the browser will always display the "save as" dialog.

If I don't set Content-Disposition, the filename used in the "save as" dialog is the one in the URL that doesn't work in my case.

I also tried setting Content-Disposition to inline but the outcome is the same.

0

2 Answers 2

208

The correct way could be:

Content-Disposition: inline; filename="myfile.txt"

5
  • 9
    I'm not sure what you meant by 'not widely supported' back when you wrote this answer, and wish you'd given hard facts instead of FUD. I just tested in IE 6, IE 8, modern Chrome, and modern Firefox, and it works in all of them; as far as I can tell, it's totally safe to use this nowadays.
    – Mark Amery
    Commented Jun 1, 2014 at 18:35
  • Does not work with my FireFox 37.0.2 and Internet Explorer 11.0.18. I'm presented with a Save as dialog. Chrome works though.
    – angularsen
    Commented May 4, 2015 at 9:05
  • 3
    Actually it does work in both Firefox 37.0.2 and IE 11.0.18 (just tested). You are probably using a Content-Type which your browser cannot display inline.
    – Patonza
    Commented May 18, 2015 at 15:01
  • 2
    A few notes also covered by various discussions over at stackoverflow.com/questions/93551/… - if you have spaces in your filename, you need to have double quotes (") around your filename. If you have non-ASCII characters in your filename, you should see that other question for a discussion of what to do. Commented Dec 3, 2015 at 18:06
  • Not working for PDF in IE11.0.96. No surprise, Chrome works just fine. Commented Mar 21, 2019 at 17:39
2

I'm not sure if that's possible by default. Due to security concerns, browsers don't send all content-types they can handle, but just a few Accept-Encoding, which doesn't help a lot in your scenario.

Maybe you can ask your user what they prefer and store that information in their profile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.