How to set response filename without forcing "save as" dialog
Asked Answered
B

2

151

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.

Baton answered 16/11, 2009 at 10:38 Comment(0)
I
206

The correct way could be:

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

Infield answered 16/11, 2009 at 11:8 Comment(5)
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.Lyublin
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.Glia
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.Infield
A few notes also covered by various discussions over at #94051 - 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.Mold
Not working for PDF in IE11.0.96. No surprise, Chrome works just fine.Downe
G
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.

Gamages answered 16/11, 2009 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.