在所有浏览器中强制保存文件-无法在浏览器窗口中打开 [英] Force save files all browsers - not open in browser window

查看:104
本文介绍了在所有浏览器中强制保存文件-无法在浏览器窗口中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种适用于所有浏览器的简单解决方案。

I'm after a simple solution to work in all browsers.

对于特定的文件类型或通过类的目标链接:如何使它们简单化

For specific file types, or targeted links via a class: how can I get them to simply force download in all major browsers.

我想我找到了适用于apachce服务器的完美解决方案-将其添加到.htaccess中。

I thought I found the perfect solution for apachce server - by adding this into the .htaccess.

http: //css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

似乎可以在Firefox和Safari中使用,但不能在chrome或IE上运行(未测试其他功能)

Seems to work in Firefox and Safari, but not chrome or IE (have not tested anything else)

可以吗请为我提供一种解决方案,该解决方案如何使所有浏览器强制链接而不是在浏览器中打开文件。

Can any one please help me with a solution on how to make links to force download the file, instead of opening in the browser, for ALL browsers.

我似乎无法查找完整的浏览器证明解决方案。

I can't seem to find a full browser proof solution. Is it not possible?

任何链接到教程或摘要的链接都很棒。

Any links to tutorial or snippets would be awesome.

我的网站(如果基于PHP)

My website if PHP based so can make it work with PHP if posible.

谢谢

推荐答案

将Content-Disposition标头设置为附件,例如(在PHP中):

Set the Content-Disposition header to "attachment", like so (in PHP):

header('Content-Disposition: attachment');

您甚至可以建议下载文件名(如果该文件名与URL中的文件名不同) :

You can even propose a filename for the download (if it differs from the one given in the URL):

header('Content-Disposition: attachment; filename=file.ext');

如果您想直接在Apache服务器中进行操作,请尝试以下操作:

If you want to do it directly in the Apache server, try this:

<FilesMatch "\.(pdf|csv|xls)">
  Header set Content-Disposition attachment
</FilesMatch>

(您可以在FilesMatch模式中添加更多扩展名)

(you may add more extensions in the FilesMatch pattern)

如果可以的话,尽量避免使用application / octet-stream头,也就是说,如果Content-Disposition没有它,也可以使用。这是因为某些系统实际上会注意Content-Type并将其与文件一起保存。如果用户的系统可以找出文件的正确MIME类型,通常对用户来说更好。

Try to avoid using the application/octet-stream header if you can, that is, if Content-Disposition works without it. That's because some systems actually pay attention to the Content-Type and saves it together with the file. It's usually better for the user if their system can figure out the correct MIME type of the file.

这篇关于在所有浏览器中强制保存文件-无法在浏览器窗口中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆