如何设置响应文件名而不强制“另存为”对话

发布于 2024-08-11 10:20:53 字数 508 浏览 6 评论 0原文

我在某些响应中返回一个流,设置适当的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被你宠の有点坏 2024-08-18 10:20:53

正确的方法可能是:

Content-Disposition: inline;文件名=“myfile.txt”

The correct way could be:

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

太傻旳人生 2024-08-18 10:20:53

我不确定默认情况下是否可以。出于安全考虑,浏览器不会发送它们可以处理的所有内容类型,而只会发送一些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文