通过标记或JS强制下载 [英] Force download through markup or JS

查看:147
本文介绍了通过标记或JS强制下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在CDN上有一个文件(来自Rackspace的云文件)和一个带有指向该文件的链接的静态html页面。有什么办法可以强制下载这个文件(以防止它在浏览器中打开 - 例如mp3)?

我们可以让我们的服务器读取文件并将相应的标题设置为:

  header(Content-Type:application / force-download)

但我们每月有大约500万次下载,所以我们宁愿让CDN照顾它。



有什么想法?

解决方案

JavaScript。 现在有了! (Ish。See @ BruceAldrige的答案在下面。)



HTTP Content-Disposition 头是什么告诉浏览器下载文件,并由服务器发送。您必须将CDN配置为将您希望浏览器下载的文件发送至该头,而不是显示。



无用的是,我完全不熟悉Rackspace的Cloud Files服务,所以我不知道他们是否允许这样做,也不知道如何去做。刚刚在2009年12月发现了一个页面,表示没有想到,可悲的是:


Cloud文件无法用Content-Disposition:attachment 'HTTP标头。因此,可以在任何其他服务中完美工作的下载链接可能会导致浏览器直接渲染文件。 Rackspace的工程师证实了这一点。 : - (



http://drupal.org/我知道您可以使用亚马逊的CloudFront服务,因为它支持S3(请参阅)。 href =http://blog.cloudberrylab.com/2009/06/how-to-set-custom-http-headers-for.html =nofollow noreferrer> http://blog.cloudberrylab.com/ 2009/06 / how-to-set-custom-http-headers-for.html

Lets assume I have a file on a CDN (Cloud Files from Rackspace) and a static html page with a link to that file. Is there any way I can force download this file (to prevent it from opening in the browser -- for mp3s for example)?

We could make our server read the file and set the corresponding header to:

header("Content-Type: application/force-download")

but we have about 5 million downloads per month so we would rather let the CDN take care of that.

Any ideas?

解决方案

There’s no way to do this in HTML or JavaScript. There is now! (Ish. See @BruceAldrige’s answer below.)

The HTTP Content-Disposition header is what tells browsers to download the files, and that’s sent by the server. You have to configure the CDN to send that header with whichever files you want to browser to download instead of display.

Unhelpfully, I’m entirely unfamiliar with Rackspace’s Cloud Files service, so I don’t know if they allow this, nor how to do it. Just found a page from December 2009 that suggests not thought, sadly:

Cloud Files cannot serve a file with the 'Content-Disposition: attachment' HTTP header. Therefore, a download link that would work perfectly in any other service may result in the browser rendering the file directly. This was confirmed by Rackspace engineers. :-(

http://drupal.org/node/656714

I know that you can with Amazon’s CloudFront service, as it’s backed by S3 (see e.g. http://blog.cloudberrylab.com/2009/06/how-to-set-custom-http-headers-for.html)

这篇关于通过标记或JS强制下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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