Html 文件打开而不是在 Internet Explorer 中的 href 链接中下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17968837/
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
File opens instead of downloading in internet explorer in a href link
提问by Ebenezar John Paul
<a href="path/to/file/filename.xxx" download="filename.xxx">filename</a>'
When i click the link, my filename.xxx should be downloaded.
当我单击链接时,应该下载我的 filename.xxx。
It works perfectly in chrome. But in Internet explorer, it opens the file instead of downloading. What could be the problem? Is there any properties that is to be added to make it work in ie.
它在 chrome 中完美运行。但是在 Internet Explorer 中,它会打开文件而不是下载文件。可能是什么问题呢?是否有任何要添加的属性以使其在 ie 中工作。
And also i need a file download sample that works for all the browsers.
而且我还需要一个适用于所有浏览器的文件下载示例。
回答by Chris
The downloadattribute is not supported in IE (see http://caniuse.com/#search=download%20attribute).
该下载属性不支持IE(见http://caniuse.com/#search=download%20attribute)。
That suggests the download attribute is only supported by firefox, chrome, opera and the latest version of blackberry's browser.
这表明下载属性仅受 firefox、chrome、opera 和最新版本的黑莓浏览器支持。
For other browsers you'll need to use more traditional methods to force download. That is server side code is necessary to set an appropriate Content-Type and Content-Disposition header to tell (or trick depending on your point of view) the browser to download the item. Headers should look like this:
对于其他浏览器,您需要使用更传统的方法来强制下载。也就是说,服务器端代码对于设置适当的 Content-Type 和 Content-Disposition 标头来告诉(或根据您的观点进行欺骗)浏览器下载项目是必要的。标题应如下所示:
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
(thanks to antyrat for the copy and paste of the headers)
(感谢 antyrat 复制和粘贴标题)
回答by antyrat
It should be fixed on server side. Your server should return this headers for this file types:
它应该固定在服务器端。您的服务器应该为此文件类型返回此标头:
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
回答by gerardw
For apache2 server:
对于 apache2 服务器:
AddType application/octect-stream .ova
File location will depend on particular version of Apache2 -- ours is in /etc/apache2/mods-available/mime.conf
文件位置将取决于 Apache2 的特定版本——我们的位于 /etc/apache2/mods-available/mime.conf
Reference:
参考:
https://askubuntu.com/questions/610645/how-to-configure-apache2-to-download-files-directly
https://askubuntu.com/questions/610645/how-to-configure-apache2-to-download-files-directly
回答by Bud Damyanov
It is known HTTP headers problem with Internet Explorer. Try to edit your server's .htaccess
file (if you use Apache) and include the following rules:
众所周知,Internet Explorer 的 HTTP 标头问题。尝试编辑您服务器的.htaccess
文件(如果您使用 Apache)并包含以下规则:
# IE: force download of .xxx files
AddType application/octect-stream .xxx
<Files *.xxx>
ForceType application/octet-stream
Header Set Content-Disposition attachment
</Files>
回答by AnaMaria
This is not a code issue. It is your default IE settings
这不是代码问题。这是您的默认 IE 设置
To change the "always open" setting:
要更改“始终打开”设置:
- In Windows Explorer, click on the "Tools" menu, choose "Folder options"
- In the window that appears, click on the "File Types" tab, and scroll through the list until you find the file extension you want to change (they're in alphabetical order). For example, if Internet Explorer always tries to open .zip files, scroll through the list until you find the entry for "zip".
- Click on the file type, then the "Advanced" button.
- Check the "Confirm after download" box, then click OK > Close.
- 在 Windows 资源管理器中,单击“工具”菜单,选择“文件夹选项”
- 在出现的窗口中,单击“文件类型”选项卡,然后滚动列表,直到找到要更改的文件扩展名(按字母顺序排列)。例如,如果 Internet Explorer 总是尝试打开 .zip 文件,请滚动列表直到找到“zip”条目。
- 单击文件类型,然后单击“高级”按钮。
- 选中“下载后确认”框,然后单击“确定”>“关闭”。
EDIT:If you ask me , instead of making any changes in the code i would add the following text "Internet Explorer users:To download file, "Rightclick" the link and hit "Save target as" to download the file."
编辑:如果您问我,我不会对代码进行任何更改,而是添加以下文本“ Internet Explorer 用户:要下载文件,”右键单击“链接并点击“目标另存为”以下载文件。
EDIT 2:THIS solution will work perfectly for you. Its a solution i just copied from the other answer. Im not trying to pass it off as my own
编辑 2:此解决方案非常适合您。这是我刚刚从另一个答案中复制的解决方案。我不想把它当作我自己的
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
However you must make sure that you specify the type of file(s) you allow. You have mentioned in your post that you want this for any type of file. This will be an issue.
但是,您必须确保指定允许的文件类型。您在帖子中提到过,您希望将其用于任何类型的文件。这将是一个问题。
For ex. If your site has images and if the end user clicks these images then they will be downloaded on his computer instead of opening in a new page. Got the point. So you need to specify the file extensions.
例如。如果您的站点有图片,并且如果最终用户单击这些图片,那么它们将被下载到他的计算机上,而不是在新页面中打开。明白了。所以你需要指定文件扩展名。
回答by Jason
Zip your file (.zip) and IE will give the user the option to open or download the file.
压缩您的文件 (.zip),IE 将为用户提供打开或下载文件的选项。
回答by Armel Larcier
This must be a matter of http headers.
这一定是 http 标头的问题。
see here: HTTP Headers for File Downloads
请参见此处: 文件下载的 HTTP 标头
The server should tell your browser to download the file by sending
服务器应该告诉您的浏览器通过发送来下载文件
Content-Type: application/octet-stream;
Content-Disposition: attachment;
in the headers
在标题中
回答by Stimpson Cat
You could configure this in your http-Header
你可以在你的 http-Header 中配置它
httpResponse.setHeader("Content-Type", "application/force-download");
httpResponse.setHeader("Content-Disposition",
"attachment;filename="
+ "MyFile.pdf");