二進制文件下載
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);