开发者

How to set response filename without forcing "save as" dialog

开发者 https://www.devze.com 2022-12-11 18:49 出处:网络
I am returning a stream in some response setting the appropriate content-type header. The behavior I\'m looking for is this:

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.


The correct way could be:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号