将 MIME 类型添加到 HTML 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2110269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add mime type to HTML link
提问by Cimm
I know how to change the MIME type in a webserver. I used this to make sure the browser downloads my .scrpt file instead of opening the plain text version. So far so good but is it possible to do the same with a link? I would like to link to a file on GitHub but this will open as a plain text file. Can I add a "MIME type attribute" to the link to tell the browser to download the file?
我知道如何更改网络服务器中的 MIME 类型。我用它来确保浏览器下载我的 .scrpt 文件而不是打开纯文本版本。到目前为止一切顺利,但是否有可能对链接做同样的事情?我想链接到 GitHub 上的文件,但这将作为纯文本文件打开。我可以在链接中添加“MIME 类型属性”以告诉浏览器下载文件吗?
This is what I would like to see:
这是我想看到的:
<a mimetype="application/octet-stream" href="http://gist.github.com/raw/279094/39d5a2c1037288d5ee0ba1a17dca9edb368bbe42/RepairiPhotoDates.scpt">download</a>
回答by Cimm
Time to answer my own question. This is a really old question and it probably wasn't possible at the time but lots has changed since then. The HTML5 spec added the download
attribute:
是时候回答我自己的问题了。这是一个非常古老的问题,当时可能是不可能的,但从那时起发生了很多变化。HTML5 规范添加了download
属性:
<a href="hugepdf.pdf" download>Download file</a>
This will do exactly what I need, tell the browser to download the file instead of opening it. Thanks to Jonathan Sv?rdénfor solving my years old question!
这将完全满足我的需要,告诉浏览器下载文件而不是打开它。感谢Jonathan Sv?rdén解决了我多年的问题!
回答by Quentin
You can specify a type attribute, but the content-type sent by the server is authoritative.
您可以指定一个type 属性,但服务器发送的 content-type 是权威的。
This attribute gives an advisory hint as to the content type of the content available at the link target address. It allows user agents to opt to use a fallback mechanism rather than fetch the content if they are advised that they will get content in a content type they do not support.
此属性提供有关链接目标地址处可用内容的内容类型的咨询提示。它允许用户代理选择使用回退机制而不是获取内容,如果他们被告知他们将获得他们不支持的内容类型的内容。
Other than that, no, you can't.
除此之外,不,你不能。
回答by doppelgreener
Can you set up a middleman script which downloads the file in question to the server, then uploads it to the user with a different MIME type?
您能否设置一个中间人脚本,将有问题的文件下载到服务器,然后使用不同的 MIME 类型将其上传给用户?
回答by Martin Labuschin
HTML is not concerned about the HTTP response headers. This is absolutely a server-side problem, which has to be solved in the HTTP repsonse headers before the HTTP reponse body(/content?) is sent. Without a scripting language like Ruby or PHP there is nothing you can do.
HTML 不关心 HTTP 响应头。这绝对是服务器端的问题,必须在发送 HTTP 响应正文(/内容?)之前在 HTTP 响应标头中解决。如果没有像 Ruby 或 PHP 这样的脚本语言,您将无能为力。