We're calling on all EU-based Mozillians with iOS or iPadOS devices to help us monitor Apple’s new browser choice screens. Join the effort to hold Big Tech to account!

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

can't get html5 download to work programmatically. (Works on Chrome.)

  • 7 个回答
  • 1 人有此问题
  • 13 次查看
  • 最后回复者为 jpaxtons

more options

Example code:

var filenamepath ="http://oihelp.com/Julio Iglesias - Corazón Partío.mp3"
var filename = "Julio Iglesias - Corazón Partío.mp3"
download(filenamepath,filename)

function download(filenamepath, filename) {
     var pom = document.createElement('a');
     pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + (filenamepath))
     pom.setAttribute('download', filename);
     pom.click();
}

On Chrome, download occurs. (Julio Iglesias - Corazón Partío.mp3) On Firefox, nothing happens and can find no errors. Do not know where to look for resolution.

An html link like this:
<a href="http://oihelp.com/Julio Iglesias - Corazón Partío.mp3" download="Julio Iglesias - Corazón Partío.mp3">Download local utf-8 file by link click </a>

works fine. (both Chrome and Firefox)


edited the title of this thread

Example code: <pre><nowiki>var filenamepath ="http://oihelp.com/Julio Iglesias - Corazón Partío.mp3" var filename = "Julio Iglesias - Corazón Partío.mp3" download(filenamepath,filename) function download(filenamepath, filename) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + (filenamepath)) pom.setAttribute('download', filename); pom.click(); }</nowiki></pre> On Chrome, download occurs. (Julio Iglesias - Corazón Partío.mp3) On Firefox, nothing happens and can find no errors. Do not know where to look for resolution. <pre><nowiki>An html link like this: <a href="http://oihelp.com/Julio Iglesias - Corazón Partío.mp3" download="Julio Iglesias - Corazón Partío.mp3">Download local utf-8 file by link click </a></nowiki></pre> works fine. (both Chrome and Firefox) ''edited the title of this thread''

由the-edmeister于修改

被采纳的解决方案

I was already suspecting something like that.
You only create the element, but aren't placing it in the DOM (e.g. document.body.appendChild(pom)) and in that case the click probably doesn't work.

定位到答案原位置 👍 3

所有回复 (7)

more options

Well, I wish I could edit my post! First, the title is wrong.. should be programmatically not problematically

Anyhow. Also, missing a ; after the ...mp3";

Also, I have tried "encodeURIComponent(filename)" but made no difference (Chrome worked, Firefox did not).

I'm guessing a different parameter in place of 'data:text/plain' might help, but so far have not found it.

Thanks, Have fun, Paxton

more options

Did you try to escape the spaces in the name as %20 (%2520).

It is usually best to avoid spaces and use underscores instead.

more options

Thank you. Perhaps you did not see my edited note. Not only escaped, but did the full URI encoding. Note: works without escaping when using the link .

So, must be something else. Thanks for the thought.

Have fun, Paxton

more options

Greetings! For what it is worth, for anyone following my saga, the problem seems to be the creation of the ancher <a> element.

I find that if the element is created in the html page and made hidden and given an id then I can set the href and download attributes and use click() to activate the link and commence the download.

No % escaping of the filenames required even for utf-8 characters. In fact the download attribute wants to contain the exact filename the user will save on his machine.

So, with a previosly created anchor element with an id of dllink....

           var filenamePath = "http://oihelp.com/Julio Iglesias - Día a Día.mp3";
           var filenameDisplay = "Julio Iglesias - Día a Día.mp3";
           document.getElementById("dllink").href = filenamePath;
           document.getElementById("dllink").download = filenameDisplay;
           var obj = document.getElementById("dllink");
           obj.click();

Works just fine. and this works in Chrome but does nothing in Firefox.

           var filenamePath = "http://oihelp.com/Julio Iglesias - Día a Día.mp3";
           var filenameDisplay = "Julio Iglesias - Día a Día.mp3";
           var pom = document.createElement('a');
           pom.href = filenamePath;
           pom.download = filenameDisplay; //works in chrome, not FF
           pom.click();
more options

选择的解决方案

I was already suspecting something like that.
You only create the element, but aren't placing it in the DOM (e.g. document.body.appendChild(pom)) and in that case the click probably doesn't work.

more options

Bingo!

Should have seen that. It works just fine. It looks like Chrome is allowing me to be sloppy :-)

Paxton

more options

Greetings!

For anyone that is following this, this works to download utf-8 filenames....

var filenamePath = "http://oihelp.com/Julio Iglesias - Día a Día.mp3";
 var filenameDisplay = "Julio Iglesias - Día a Día.mp3";
 var pom = document.createElement('a');
 document.body.appendChild(pom); //required in FF, optional for Chrome
          pom.href = filenamePath;
          pom.download = filenameDisplay;
          pom.target="_self" ; //required in FF, optional for Chrome
          pom.click();


Have fun.