简体   繁体   English

文件下载重命名不起作用

[英]File download rename not working

I try to rename file with download attribute but it's not working. 我尝试使用下载属性重命名文件,但是它不起作用。

<a href="https://jsfiddle.net/img/logo.png" download="something.png">OK</a>

FIDDLE 小提琴

From the docs you linked: 从您链接的文档中:

If the HTTP header Content-Disposition: is present and gives a different filename than this attribute, the HTTP header has priority over this attribute. 如果存在HTTP标头Content-Disposition:,并且文件名与此属性不同,则HTTP标头的优先级高于此属性。

My guess is that the server you're linking to sets this header. 我的猜测是您要链接的服务器设置了此标头。

Also if you're linking to an external resource it likely won't work: 另外,如果您要链接到外部资源,则可能无法正常工作:

This attribute is only honored for links to resources with the same-origin. 仅对具有相同来源的资源的链接授予此属性。

It only works if the file is on the same origin so if you can download a external file with CORS + ajax then you can save the blob with a custom name 仅当文件源相同时才起作用,因此,如果您可以使用CORS + ajax下载外部文件,则可以使用自定义名称保存Blob

 $('a').click(function(evt){ evt.preventDefault(); var name = this.download; // we need a blob so we can create a objectURL and use it on a link element // jQuery don't support responseType = 'blob' (yet) // So I use the next version of ajax only avalible in blink & firefox // it also works fine by using XMLHttpRequest v2 and set the responseType fetch("https://crossorigin.me/" + this.href) // res is the beginning of a request it only gets the response headers // here you can use .blob() .text() .json or res.arrayBuffer() depending // on what you need, if it contains Content-Type: application/json // then you might want to choose res.json() // all this returns a promise .then(res => res.blob()) .then(blob => { $("<a>").attr({ download: name, href: URL.createObjectURL(blob) })[0].click(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="https://jsfiddle.net/img/logo.png" download="something.png">OK</a> 

It appears that this attribute doesn't work anymore for external files, due to possible security concerns. 由于可能的安全问题,该属性似乎不再对外部文件起作用。

You can find a discussion about this issue for Chrome here 您可以在此处找到有关Chrome的此问题的讨论

Use of the 'download' attribute will always trigger a download, but from M-35 onwards will only honor the suggested filename if the final resource URL is same-origin as the document. 使用“下载”属性将始终触发下载,但是从M-35开始,仅当最终资源URL与文档具有相同的来源时,才会使用建议的文件名。 Even when it doesn't, as long as a MIME type is specified correctly, it will receive a filename like 'download.' 即使没有指定,只要正确指定了MIME类型,它也会收到“下载”之类的文件名。 where is the extension known to the host OS as mapping to the specified MIME type. 主机操作系统将扩展名映射为指定的MIME类型。 If the resource is served with a Content-Disposition, then the Content-Disposition will take precedence. 如果资源与Content-Disposition一起提供,则Content-Disposition将优先。

And for Firefox here and here 对于Firefox而言, 这里这里

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

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