Download data url file

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

    Here is a pure JavaScript solution I tested working in Firefox and Chrome but not in Internet Explorer:

    function downloadDataUrlFromJavascript(filename, dataUrl) {
    
        // Construct the 'a' element
        var link = document.createElement("a");
        link.download = filename;
        link.target = "_blank";
    
        // Construct the URI
        link.href = dataUrl;
        document.body.appendChild(link);
        link.click();
    
        // Cleanup the DOM
        document.body.removeChild(link);
        delete link;
    }
    

    Cross-browser solutions found up until now:

    downloadify -> Requires Flash

    databounce -> Tested in IE 10 and 11, and doesn't work for me. Requires a servlet and some customization. (Incorrectly detects navigator. I had to set IE in compatibility mode to test, default charset in servlet, JavaScript options object with correct servlet path for absolute paths...) For non-IE browsers, it opens the file in the same window.

    download.js -> http://danml.com/download.html Another library similar but not tested. Claims to be pure JavaScript, not requiring servlet nor Flash, but doesn't work on IE <= 9.

提交回复
热议问题