简体   繁体   English

是否可以在远程服务器上创建文件的下载链接?

[英]Is it possible to make a download link for file on remote server?

Is it possible to create a download link for a remote file in plain HTML, or with JavaScript or jQuery? 是否可以使用纯HTML或JavaScript或jQuery为远程文件创建下载链接?

The download attribute doesn't seem to work for remote files in Chrome 73 or Firefox 66. download属性似乎不适用于Chrome 73或Firefox 66中的远程文件。

<a href="//amazon.com/ads.txt" download>ads.txt</a> 

No, the file URL must be on the same domain as the containing document, unless it's a blob: or data: URL: 不,文件URL必须与包含文档位于同一个域中,除非它是blob:data: URL:

  • This attribute only works for same-origin URLs. 此属性仅适用于同源URL。
  • Although HTTP(s) URLs need to be in the same-origin, blob: URLs and data: URLs are allowed so that content generated by JavaScript, such as pictures created in an image-editor Web app, can be downloaded. 虽然HTTP(s)URL需要位于同源,但是允许使用blob:URL和data:URL,以便可以下载JavaScript生成的内容,例如在图像编辑器Web应用程序中创建的图片。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

As far as I know it's not possible. 据我所知,这是不可能的。 I would recommend using some sort of proxy-script on your server to overcome the cross-domains issues, like a simple PHP script. 我建议在服务器上使用某种代理脚本来克服跨域问题,比如一个简单的PHP脚本。 You can check referrer, add custom headers for content disposition etc 您可以检查referrer,为内容处理添加自定义标头等

You can't do this client side. 你不能做这个客户端。

The server hosting the resource you want to download can provide a Content-Disposition response header which will trigger a download. 托管您要下载的资源的服务器可以提供Content-Disposition响应标头 ,该标头将触发下载。

Content-Disposition: attachment; filename=ads.txt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM