Download data url file

前端 未结 9 1426
猫巷女王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");

    0 讨论(0)
  • 2020-11-22 09:49

    There are several solutions but they depend on HTML5 and haven't been implemented completely in some browsers yet. Examples below were tested in Chrome and Firefox (partly works).

    1. Canvas example with save to file support. Just set your document.location.href to the data URI.
    2. Anchor download example. It uses <a href="your-data-uri" download="filename.txt"> to specify file name.
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-11-22 09:50

    Your problem essentially boils down to "not all browsers will support this".

    You could try a workaround and serve the unzipped files from a Flash object, but then you'd lose the JS-only purity (anyway, I'm not sure whether you currently can "drag files into browser" without some sort of Flash workaround - is that a HTML5 feature maybe?)

    0 讨论(0)
  • 2020-11-22 09:54

    Ideas:

    • Try a <a href="data:...." target="_blank"> (Untested)

    • Use downloadify instead of data URLs (would work for IE as well)

    0 讨论(0)
  • 2020-11-22 09:55

    Combining answers from @owencm and @Chazt3n, this function will allow download of text from IE11, Firefox, and Chrome. (Sorry, I don't have access to Safari or Opera, but please add a comment if you try and it works.)

    initiate_user_download = function(file_name, mime_type, text) {
        // Anything but IE works here
        if (undefined === window.navigator.msSaveOrOpenBlob) {
            var e = document.createElement('a');
            var href = 'data:' + mime_type + ';charset=utf-8,' + encodeURIComponent(text);
            e.setAttribute('href', href);
            e.setAttribute('download', file_name);
            document.body.appendChild(e);
            e.click();
            document.body.removeChild(e);
        }
        // IE-specific code
        else {
            var charCodeArr = new Array(text.length);
            for (var i = 0; i < text.length; ++i) {
                var charCode = text.charCodeAt(i);
                charCodeArr[i] = charCode;
            }
            var blob = new Blob([new Uint8Array(charCodeArr)], {type: mime_type});
            window.navigator.msSaveOrOpenBlob(blob, file_name);
        }
    }
    
    // Example:
    initiate_user_download('data.csv', 'text/csv', 'Sample,Data,Here\n1,2,3\n');
    
    0 讨论(0)
提交回复
热议问题