强制文件下载而不是在浏览器里打开显示

Force Files to Download Instead of Showing Up in the Browser

强制文件下载而不是在浏览器里打开显示

 

Usually when a user goes goes to a file URL, the file will show in the browser if the browser supports it. Image files like png, gif, jpg almost always show in the browser. Archive files like zip, tar, and gzip almost are always downloaded. Some file types show up in some browsers but not others, svg files will display in Firefox and Safari, but not Internet Explorer 7. Internet Explorer will usually try to show Microsoft Word files (doc and docx) in the browser, while most other browsers will download it.

一般情况下用户点击文件连接,浏览器会直接打开文件而不是下载。不同的文件类型在不同的浏览器下会有不同的打开方式,具体的看上面的文字。

To add consistency or to force certain files to download, you can fix this by simply adding an .htaccess file to the files directory. Note this only works with Public downloads and an Apache web server.

要强制文件下载,简单地在文件存放路径加一个.htaccess文件即可(注:此方法只在apache服务器下,使用public downloads有效)。

 

  • First, setup your file field so that it saves into a separate directory. This will make it so that you will only force certain files to download and not affect the rest of the files on your Drupal site. Visit Administer -> Content management -> Content types (admin/content/types). Click "Manage fields" on the type that you want files to be forced to be downloaded. Add or edit a file field, under the "Path settings" fieldset, set a path for your files to be uploaded.

    Now files uploaded to that field will be saved in a separate folder under your sites files directory.

            首先,设置你的文件字段,让它保存到一个独立的目录里。

            Administer-->Content management -> Content types,点击要设置下载的file字段后面的“管理字段”按钮,在“设置路径”那里,设置一个供保存文件的独立路径。现在你的文件都保存在这个路径下了

  • FTP to your site and create a file named .htaccess in your files directory. Usually this will be sites/default/files/[name of the subdirectory from step 1]. In that file put the following lines:

            这个路径下增加一个.htaccess文件,里面加如下内容:

<FilesMatch "\.(?i:doc|odf|pdf|rtf|txt)$">
  Header set Content-Disposition attachment
</FilesMatch>

 

This particular example will force files with doc, odf, pdf, rtf, or txt to download instead of being shown in the browser.

上面这个特例将强制doc, odf, pdf,rtf,txt这几类文件将被下载而不是在浏览器里打开。

上面的文件类别只是示例,你可以改成你自己的文件类型。

 

  • Upload a few files and (using the "Generic files" display setting), the files will be downloaded when the user clicks on the link.

            上传一个文件,并设置显示方式为“普通文件”,用户点击这个文件连接的时候将直接下载而不是在浏览器里打开了

  • You may need to load mod_headers in Apache2 for this to work.
    Assuming you have root access it can be done as follows:

            你可能需要在apache2里加裁mod_headers这个模块。

            假设你的权限,按下面这样做:

# a2enmod headers
# /etc/init.d/apache2 reload

 

原文地址:https://drupal.org/node/417866

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值