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
标签:msSaveBlob,aLink,js,blob,var,new,fileName,下载 来源: https://www.cnblogs.com/laplasedemon/p/14951079.html