Html 如何强制下载 MIME 类型的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7229361/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:20:07  来源:igfitidea点击:

How to force mime-type of file download

htmliframemime-types

提问by S B

When users visit our website's download page, it automatically starts the download. The page has a display:hiddeniframe with srcpointing to the installer file

当用户访问我们网站的下载页面时,它会自动开始下载。该页面有一个指向安装程序文件的display:hiddeniframesrc

<iframe src="/path/to/installer.dmg"></iframe>

This works fine so far. But my Chrome extension: "Web developer" logs this Warning message

到目前为止,这工作正常。但是我的 Chrome 扩展程序:“Web 开发人员”会记录此警告消息

Resource interpreted as Document but transferred with MIME type application/octet-stream

Resource interpreted as Document but transferred with MIME type application/octet-stream

So, is there a way to explicitly declare installer.dmg's content-type as octet-stream, so that browsers do not get confused?

那么,有没有办法将 installer.dmg 的内容类型显式声明为八位字节流,以便浏览器不会混淆?

回答by Chris Harrison

As one of the previous answers references a broken link I will give my answer here.

由于之前的答案之一引用了一个断开的链接,我将在这里给出我的答案。

If you are trying to specify the mime type of files with a certain extension you can add this to .htaccess:

如果您尝试使用特定扩展名指定文件的 mime 类型,您可以将其添加到 .htaccess 中:

AddType application/octet-stream .dmg

However, this will not guarantee that browsers will 'download' the file. Chrome for example does not recognise this. So here is a way to force files of a certain type to 'download':

但是,这并不能保证浏览器会“下载”该文件。例如,Chrome 无法识别这一点。所以这是一种强制某种类型的文件“下载”的方法:

<FilesMatch "\.(?i:dmg)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

The little bit of regex around the extension (dmg) is just there to make it case insensitive.

扩展名 (dmg) 周围的一点正则表达式只是为了使其不区分大小写。

You might need to clear browser cache before it works correctly.

您可能需要清除浏览器缓存才能正常工作。

This works in the latest Chrome, Firefox and IE8 (as of August 2013). I have not tested in the latest IE or Safari, so if someone has those browsers and can test them, please confirm they work in the comments below.

这适用于最新的 Chrome、Firefox 和 IE8(截至 2013 年 8 月)。我没有在最新的 IE 或 Safari 中测试过,所以如果有人拥有这些浏览器并且可以测试它们,请在下面的评论中确认它们是否有效。

回答by Mike Crawford

This pagehas what you need, if your web server is Apache. Basically what you do is tell apache that any file with a .dmg extension has the mime-type of application/octet-stream.

如果您的 Web 服务器是 Apache,此页面有您需要的内容。基本上你要做的是告诉 apache 任何带有 .dmg 扩展名的文件都具有应用程序/八位字节流的 MIME 类型。

回答by user11713544

Insert those lines in your vhost configuration, inside a docroot directory:

将这些行插入到您的 vhost 配置中的 docroot 目录中:

            <Directory path/of/your/docroot/directory>
                <Files path/of/your/file>
                     Header set Content-type "mime/type"
                </Files>
            </Directory>

回答by kleinohad

The problem is that you are using an installer file as a web page what you can do is open the installer page in a popup on document ready you can read this (with jquery): http://choosedaily.com/1178/15-jquery-popup-modal-dialog-plugins-tutorials/or this(no jquery): http://www.quirksmode.org/js/popup.html

问题是您将安装程序文件用作网页,您可以做的是在准备好文档的弹出窗口中打开安装程序页面,您可以阅读此内容(使用 jquery):http: //choosedaily.com/1178/15- jquery-popup-modal-dialog-plugins-tutorials/或这个(没有 jquery):http: //www.quirksmode.org/js/popup.html