Firefox 和 Content-Disposition 标头

发布于 2025-01-02 17:38:50 字数 540 浏览 1 评论 0原文

我对附件名称有疑问。当我在谷歌浏览器上调用该网站时,它会返回具有正确名称和扩展名的文件。我用 Internet Explorer 测试了一下,效果也很好。该问题仅存在于 Firefox 中。我致电该网站,它返回文件标题的第一个单词,但没有扩展名。

例如,如果我想要一个名为“My report.docx”的文件,它会变成一个名为“My”的文件。我用谷歌搜索了一下,结果发现这是人们的一个常见问题,因为浏览器读取标题的方式不同。他们说修复方法是引用文件名:

Content-Disposition:附件; filename=My Report.docx

现在是:(注意引号)

Content-Disposition:附件; filename="My Report.docx"

但是,这对我不起作用。

在 Chrome 上,它返回“My Report.docx”(实际上带有引号)。 Firefox 返回了一个奇怪的文件,该文件具有正确的扩展名和正确的名称,但没有引号,但无法执行。它是正确的文件大小、正确的扩展名和正确的名称,但无法执行。它还在文件名前后返回一个空格。

I have a problem with an attachment's name. When I call the site on google chrome it returns the file with the right name and extension. I tested it with internet explorer and it works fine too. The issue lies with only Firefox. I call the site and it returns the first word on the file title and no extension.

For example if I wanted a file called "My report.docx" it turns a file called "My". I Googled around and it turns out this is a common issue with people because browsers read the headers differently. They said the fix is to quote the file name:

Content-Disposition: attachment; filename=My Report.docx

is now: (note the quotes)

Content-Disposition: attachment; filename="My Report.docx"

However, that did not work for me.

On chrome it returned "My Report.docx" (actually with the quotes). Firefox returned a odd file that had the proper extension and proper name and no quotes yet it could not be executed. It was the proper file size, proper extension, and proper name yet it could not be executed. Also it returns a space before and after the file name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一个人的夜不怕黑 2025-01-09 17:38:50

我知道这是一个很老的问题,但我最近遇到了同样的问题。解决方案是按照

  1. RFC2184 对文件名进行编码,或者,
  2. 如果您不这样做文件名中包含特殊字符,请在内容配置字符串中引用它。

由于您已经尝试过 2,因此您可以尝试使用 1 并看看效果如何。

通常我使用 ContentDisposition 类为我生成标头:

Dim contentDispositionHeader = New Net.Mime.ContentDisposition() With {.FileName = filename}
Response.AddHeader("Content-Disposition", contentDispositionHeader.ToString())

希望这会有所帮助。

I know this is a very old question, but I was recently having the same problem. The solution is to either

  1. Encode your filename per RFC2184 or,
  2. If you don't have special characters in your filename, quote it in the content disposition string.

Since you've already tried 2, you could try using 1 and seeing how that works out.

Usually I use the ContentDisposition class to generate my header for me:

Dim contentDispositionHeader = New Net.Mime.ContentDisposition() With {.FileName = filename}
Response.AddHeader("Content-Disposition", contentDispositionHeader.ToString())

Hope this helps.

白色秋天 2025-01-09 17:38:50

这应该按预期工作,这是另一个具有相同问题的 SOq:

以及 Mozilla 页面(我猜您正在引用这个):

我不知道您的服务器端代码的具体情况,但这里有一些需要确认/尝试的事情:

  • 如果您的服务器上有可用的 PHP,您可以尝试一下吗上面第一个链接中的代码?如果没有,您可能可以在网上找到您选择的语言的内容。这样,您就可以确认问题是否出在您的代码中或其他地方(服务器设置、浏览器等)。
  • 这种情况是发生在其他客户端计算机上(即您尝试下载的位置)还是仅发生在该客户端计算机上?您可能想尝试其他人来确认。
  • 这在 IE / Safari 或其他浏览器中工作正常吗?您甚至可以尝试从命令行或类似的方式使用 wgetcurl 来执行此操作。
  • 您是否还正确提供了 Content-Type 标头?
  • 您可以尝试下载其他文件或不同类型的文件,例如 .png.xls 吗?事实上,最简单的方法可能是尝试纯文本文件 (text/plain),然后从那里获取它。

希望这有帮助。

This should work as expected, here's another SOq with the same problem:

and also the Mozilla page (I guess you were referencing this one):

I don't know the specifics of your server side code, but here are some things to confirm / try:

  • If you have PHP available at the server, can you try the code from the first link above? If not, you can probably find something on the Net in your language of choice. That way, you can confirm whether the issue is in your code or somewhere else (server setup, browser, etc.)
  • Is this happening on other client machines (i.e. where you try the download from) or only on that one? You might want to try others to confirm.
  • Is this working fine in IE / Safari or some other browser? You can even try doing it with wget or curl from the command line or something like that.
  • Are you also providing the Content-Type header correctly?
  • Can you try downloading some other file or a file of a different type, e.g. a .png or a .xls? In fact, probably the easiest would be to try a plain text file (text/plain) and then take it from there.

Hope this helps.

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