我们在做前后端对接时,经常会遇到要做图表导出的功能,这里分享一个前端接收ArrayBuffer转为Blob并下载为excel的代码段,兼容IE、Chrome、Firefox
export const downLoadXls = (fileArrayBuffer, filename) => { let data = new Blob([fileArrayBuffer], { type: 'application/vnd.ms-excel,charset=utf-8' }); if (typeof window.chrome !== 'undefined') { // Chrome var link = document.createElement('a'); link.href = window.URL.createObjectURL(data); link.download = filename; link.click(); } else if (typeof window.navigator.msSaveBlob !== 'undefined') { // IE var blob = new Blob([data], { type: 'application/force-download' }); window.navigator.msSaveBlob(blob, filename); } else { // Firefox var file = new File([data], filename, { type: 'application/force-download' }); window.open(URL.createObjectURL(file)); } }
本文为胖虎原创文章,转载无需和我联系,但请注明来自胖虎博客panghucat.cn