Prohledat stránky podpory

Vyhněte se podvodům. Za účelem poskytnutí podpory vás nikdy nežádáme, abyste zavolali nebo poslali SMS na nějaké telefonní číslo nebo abyste sdělili své osobní údaje. Jakékoliv podezřelé chování nám prosím nahlaste pomocí odkazu „Nahlásit zneužití“.

Zjistit více

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

  • 7 odpovědí
  • 1 má tento problém
  • 18 zobrazení
  • Poslední odpověď od 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''

Upravil uživatel the-edmeister dne

Zvolené řešení

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.

Přečíst dotaz v kontextu 👍 3

Všechny odpovědi (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

Zvolené řešení

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.