How to set response filename without forcing savea

2019-01-12 23:58发布

问题:

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 saveas dialog where the filename should be one provided in the response.

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

"attachment; filename="myfile.txt""

the browser will always display the savesas dialog.

If i don't set the Content-Disposition the filename used in the saveas dialog is the one in the url that doesn't work in my case.

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

回答1:

The correct way could be:

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



回答2:

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

Maybe you can to ask your user what he/she prefer, and store that information in their profile.