xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

html5 download blob




https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link

https://stackoverflow.com/questions/6165266/downloading-mp3-files-using-html5-blobs-in-a-chrome-extension

https://codepen.io/vidhill/pen/bNPEmX

html 5 Simple File Blob Download

demo


// <a id="myButton" href="#">Download JSON</button>
// <button id="btn">Download JSON</button>

// dummy json data to save
const data = {
    x: 42,
    s: "hello, world",
    d: new Date()
};

let btn = document.getElementById("btn");

btn.addEventListener(`click`, (e) => {
    let json = JSON.stringify(data),
        // Blob
        blob = new Blob([json], {type: "octet/stream"}),
        // blob to url
        url = window.URL.createObjectURL(blob);
    this.href = url;
    this.download = "api.json";
    this.target = "_blank";
    // target filename
});


原文地址:https://www.cnblogs.com/xgqfrms/p/9921168.html