This site will have limited functionality while we undergo maintenance to improve your experience. If an article doesn't solve your issue and you want to ask a question, we have our support community waiting to help you at @FirefoxSupport on Twitter and/r/firefox on Reddit.

Caută ajutor

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Află mai multe

Acest fir de discuție a fost arhivat. Adresează o întrebare nouă dacă ai nevoie de ajutor.

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

  • 7 răspunsuri
  • 1 are această problemă
  • 18 vizualizări
  • Ultimul răspuns de 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''

Modificat în de the-edmeister

Soluție aleasă

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.

Citește acest răspuns în context 👍 3

Toate răspunsurile (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

Soluție aleasă

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.