Download data url file

前端 未结 9 1550
猫巷女王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:48

    function download(dataurl, filename) {
      var a = document.createElement("a");
      a.href = dataurl;
      a.setAttribute("download", filename);
      a.click();
    }
    
    download("data:text/html,HelloWorld!", "helloWorld.txt");

    or:

    function download(url, filename) {
    fetch(url).then(function(t) {
        return t.blob().then((b)=>{
            var a = document.createElement("a");
            a.href = URL.createObjectURL(b);
            a.setAttribute("download", filename);
            a.click();
        }
        );
    });
    }
    
    download("https://get.geojs.io/v1/ip/geo.json","geoip.json")
    download("data:text/html,HelloWorld!", "helloWorld.txt");

提交回复
热议问题