Join the AMA (Ask Me Anything) with the Firefox leadership team to celebrate Firefox 20th anniversary and discuss Firefox’s future on Mozilla Connect. Mark your calendar on Thursday, November 14, 18:00 - 20:00 UTC!

為了改善您的使用體驗,本網站正在進行維護,部分功能暫時無法使用。若本站的文件無法解決您的問題,想要向社群發問的話,請到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 發問,我們的社群成員將很快會回覆您的疑問。

搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

了解更多

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.