我這里只是簡單的記錄了下載的用法,需要上傳功能的話可以看文章下面的鏈接,里面有Blob更詳細的用法
封裝方法
function getExel(url, params) {
// 注意:responseType應設置為:'arraybuffer' or 'blob' return new Promise(function(resolve, reject) { let data = { method: "GET", url:url, headers: { 'token': gettoken("token") }, responseType: 'arraybuffer' } resolve(axios(data)); }) }
發送請求($Api已經掛載在了vue對象上,所以可以這么使用)
(關於因為Blob對象中的type屬性通常是 MIME 類型,所以這里給大家一個鏈接可以找對應文件的MIME類型:https://www.w3school.com.cn/media/media_mimeref.asp)
this.$Api.getExel("/goodsCheckService/v1/planMaterial/export?idList="+idArray) .then(response => { let a = document.createElement('a'); //ArrayBuffer 轉為 Blob let blob = new Blob([response.data], {type: "application/vnd.ms-excel"}); let objectUrl = URL.createObjectURL(blob); a.setAttribute("href",objectUrl); a.setAttribute("download", 'template.xls'); a.click(); });
(注:以上內容參考鏈接:https://blog.csdn.net/qq_37899792/article/details/90748268)
關於Blob更詳細的用法:https://blog.csdn.net/z591102/article/details/107530381
(一位網友寫的,覺得很不錯,推薦給有需要的小伙伴)