axio請求裏必須加 responseType: 'blob' 參數,以下javascript
//下載文件 api.download=function(id) { return request({ url: this.baseUrl+'/download/'+id, method: 'get', params: {}, responseType: 'blob' }) }
返回結果裏面要作以下處理java
.then( res => { let blob = new Blob([res], {type: res.type}) let downloadElement = document.createElement('a') let href = window.URL.createObjectURL(blob); //建立下載的連接 downloadElement.href = href; downloadElement.download = fileName; //下載後文件名 document.body.appendChild(downloadElement); downloadElement.click(); //點擊下載 document.body.removeChild(downloadElement); //下載完成移除元素 window.URL.revokeObjectURL(href); //釋放blob對象 })