js通過blob下載文件


js通過blob來下載文件,兼容IE

var fileName = "test.txt";
var
str = "12345"; var blob = new Blob([str]); //IE瀏覽器使用msSaveBlob if (window.navigator.msSaveBlob){ window.navigator.msSaveBlob(blob, fileName); }else{ var aLink = document.createElement('a'); aLink.download = fileName; aLink.href = URL.createObjectURL(blob); aLink.click(); }

支持后台回傳buffer到前端組成文件

//buf buffer
var arrayBuffer=new ArrayBuffer(buf.length);
var bufView = new Uint8Array(arrayBuffer);
for(var i=0;i<buf.length;i++){
    bufView[i]=buf[i];
}
var blob = new Blob([arrayBuffer], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'})

也支持xml或者json


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM