1 /** 2 * get blob 下载文件方法 3 * @param {Number} time 4 * @return {String} 5 * @returns {Object} {time: Number, unit: any} 6 */ 7 export function getDownload(value: any, defaultFileName: string, type: string) { 8 const disposition = value.headers?.['content-disposition']; 9 let fileName = defaultFileName; 10 if (disposition) { 11 const reg = /filename=(.*)/; 12 fileName = decodeURI(reg.exec(disposition)[1].trim()); 13 } 14 const blob = new Blob([value.data], { type: type }); 15 const url = window.URL.createObjectURL(blob); 16 const link = document.createElement('a'); 17 link.style.display = 'none'; 18 link.href = url; 19 20 link.setAttribute('download', fileName); 21 document.body.appendChild(link); 22 link.click(); 23 document.body.removeChild(link); 24 window.URL.revokeObjectURL(url); 25 }
标签:文件,const,link,blob,disposition,数据流,document,type,下载 From: https://www.cnblogs.com/lrtroot/p/16785236.html