Blob

二进制文件下载

function download(blob) {
    // 创建一个blob链接
    let url = URL.createObjectURL(blob)
    let a = document.createElement('a')
    a.setAttribute('download', url)
    a.href=ur;
    a.style.display = 'none'
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
    // 每次调用URL.createObjectURL,都会创建一个新的URL对象,浏览器内存中会保持对该对象的引用
    // 只有在document销毁时,才会释放此部分内存
    // 在考虑性能的情况下,在url使用结束后,最好释放此部分内存
    URL.revokeObjectURL(url)
}

二进制文件读取

 var debug = { hello: "world" };
// type :默认值为 "" ,表示将会被放入到 blob 中的数组内容的 MIME 类型
 var blob = new Blob([JSON.stringify(debug, null, 2)], {type : 'application/json'})
const objectUrl = URL.createObjectURL(blob);

你可能感兴趣的:(Blob)