将 mime 类型添加到 HTML 链接
我知道如何更改网络服务器中的 MIME 类型。我用它来确保浏览器下载我的 .scrpt 文件而不是打开纯文本版本。到目前为止一切顺利,但是是否可以通过链接执行相同的操作?我想链接到 GitHub 上的文件,但这将作为纯文本文件打开。我可以在链接中添加“MIME 类型属性”来告诉浏览器下载文件吗?
这就是我想看到的:
<a mimetype="application/octet-stream" href="http://gist.github.com/raw/279094/39d5a2c1037288d5ee0ba1a17dca9edb368bbe42/RepairiPhotoDates.scpt">download</a>
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?
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是时候回答我自己的问题了。这是一个非常古老的问题,在当时可能是不可能的,但从那时起很多事情都发生了变化。 HTML5 规范添加了
download
属性:这将完全满足我的需要,告诉浏览器下载文件而不是打开它。感谢 Jonathan Svärdén 解决了我的问题老问题!
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:This will do exactly what I need, tell the browser to download the file instead of opening it. Thanks to Jonathan Svärdén for solving my years old question!
您可以指定 type 属性,但是服务器发送的content-type是权威的。
除此之外,不,你不能。
You can specify a type attribute, but the content-type sent by the server is authoritative.
Other than that, no, you can't.
您能否设置一个中间人脚本,将相关文件下载到服务器,然后将其上传给具有不同 MIME 类型的用户?
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?
HTML 不关心 HTTP 响应标头。这绝对是一个服务器端问题,必须在发送 HTTP 响应正文(/内容?)之前在 HTTP 响应标头中解决。如果没有 Ruby 或 PHP 这样的脚本语言,您将无能为力。
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.