封装axios api 导出excel文件
1 function exportExcel(method, url, data, callback) { 2 return new Promise((resolve, reject) => { 3 let ajaxParams = {} 4 ajaxParams = { 5 method: method, 6 url: url, 7 params: data, 8 headers: { 9 'Content-type': 'application/json;application/octet-stream' 10 }, 11 responseType: 'arraybuffer' // 表示服务器响应的数据类型,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream' 12 } 13 instance(ajaxParams).then(res => { 14 if(res.status == 200) { 15 const content = res.data; 16 const blob = new Blob([content], {type: 'application/vnd.ms-excel'}); 17 const elink = document.createElement('a'); 18 elink.download = `xx信息.xls`; 19 elink.style.display = 'none'; 20 elink.href = URL.createObjectURL(blob); 21 document.body.appendChild(elink); 22 elink.click(); 23 URL.revokeObjectURL(elink.href); // 释放URL 对象 24 document.body.removeChild(elink); 25 resolve(res) 26 } else { 27 Message({ 28 type: 'error', 29 message: res.data.message, 30 duration: '1800' 31 }) 32 reject(res.data) 33 } 34 }).catch(error => reject(error)) 35 }) 36 }