八位字节流数据URI忽略<a> &#39;download&#39; attribute

时间:2017-07-04 20:01:36

标签: javascript jquery html http-headers

I have a download button on my page that looks like this:

<a download="test.jpg" href="data:application/octet-stream;base64,/9j/4AAQSkZ...">Download</a>

The href attribute is populated by JavaScript, and on my test server it functions exactly as I'd expect it to: it prompts a 'Save As' dialogue box with the filename "test.jpg".

However, when I use the exact same code on a different, live server, the filename is dropped.

  • In Chrome it saves as 'download', without a file extension
  • In Firefox it saves as a seemingly random string, again without a file extension

Any ideas what could be causing this difference in behaviour across the two servers?

1 个答案:

答案 0 :(得分:1)

这是保存非现场下载时应用的安全措施。由于prototcols不匹配(数据vs http),它被认为是一个不同的网站,默认名称适用。使用URL.createObjectURL而不是base64来提供下载的href。您可以获得dataURL to Blob routine in my downlod.js library,然后将Blob提供给createObjectURL()。