如何设置响应文件名而不强制“另存为”对话
我在某些响应中返回一个流,设置适当的 content-type
标头。我正在寻找的行为是这样的:
如果浏览器能够呈现给定内容类型的内容,那么它应该在浏览器窗口中显示它。
如果浏览器不知道如何呈现内容,那么它应该显示“另存为”对话框,其中文件名应该是响应中提供的文件名。
问题是,如果我设置 Content-Disposition
标头:
“附件;文件名=“myfile.txt””
浏览器将始终显示“另存为”对话框。
如果我不设置 Content-Disposition
,“另存为”对话框中使用的文件名就是 URL 中的文件名,在我的情况下不起作用。
我还尝试将 Content-Disposition
设置为 inline
但结果是相同的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的方法可能是:
Content-Disposition: inline;文件名=“myfile.txt”
The correct way could be:
Content-Disposition: inline; filename="myfile.txt"
我不确定默认情况下是否可以。出于安全考虑,浏览器不会发送它们可以处理的所有内容类型,而只会发送一些
Accept-Encoding
,这对您的场景没有多大帮助。也许您可以询问用户他们喜欢什么,并将该信息存储在他们的个人资料中。
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.