Download data url file

前端 未结 9 1554
猫巷女王i
猫巷女王i 2020-11-22 09:34

I\'m playing with the idea of making a completely JavaScript-based zip/unzip utility that anyone can access from a browser. They can just drag their zip directly into the br

9条回答
  •  春和景丽
    2020-11-22 09:50

    If you also want to give a suggested name to the file (instead of the default 'download') you can use the following in Chrome, Firefox and some IE versions:

    function downloadURI(uri, name) {
      var link = document.createElement("a");
      link.download = name;
      link.href = uri;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      delete link;
    }
    

    And the following example shows it's use:

    downloadURI("data:text/html,HelloWorld!", "helloWorld.txt");
    

提交回复
热议问题