【vue】將后端返回的二進(jìn)制流前端進(jìn)行處理實(shí)現(xiàn)下載文件
因?yàn)楹蠖朔祷氐氖沁@樣的二進(jìn)制流,也沒(méi)有返回狀態(tài)碼,所以會(huì)被攔截,需要在request.js文件對(duì)返回的狀態(tài)碼進(jìn)行處理
因?yàn)楹蠖朔祷氐氖沁@樣的二進(jìn)制流称簿,也沒(méi)有返回狀態(tài)碼邮丰,所以會(huì)被攔截铆惑,需要在request.js文件對(duì)返回的狀態(tài)碼進(jìn)行處理
image.png
在requests.js
里面修改下
// 響應(yīng)時(shí)攔截
service.interceptors.response.use(
response => {
const res = response.data
if (res.code !== 200) {
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
MessageBox.confirm('用戶(hù)信息已過(guò)期,請(qǐng)重新登錄', '系統(tǒng)提示', {
confirmButtonText: '登錄',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
//清空sessionStorage
clearStorage();
//清空token過(guò)期時(shí)間
removeTokenTime();
location.reload()
})
})
} else if (!res.code) { // 如果沒(méi)有返回code,可能是下載excel
// return resolve(response.data, res);
return res
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
}, error => {
console.log('err' + error)
//清空sessionStorage
clearStorage();
//清空token過(guò)期時(shí)間
removeTokenTime();
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
)
Content-Type部分媒體類(lèi)型
".*"="application/octet-stream"
".001"="application/x-001"
".301"="application/x-301"
".323"="text/h323"
".906"="application/x-906"
".907"="drawing/907"
".a11"="application/x-a11"
".acp"="audio/x-mei-aac"
".ai"="application/postscript"
".aif"="audio/aiff"
".aifc"="audio/aiff"
".aiff"="audio/aiff"
".anv"="application/x-anv"
".asa"="text/asa"
".asf"="video/x-ms-asf"
".asp"="text/asp"
".asx"="video/x-ms-asf"
".au"="audio/basic"
".avi"="video/avi"
".awf"="application/vnd.adobe.workflow"
".biz"="text/xml"
".bmp"="application/x-bmp"
".bot"="application/x-bot"
".c4t"="application/x-c4t"
".c90"="application/x-c90"
".cal"="application/x-cals"
".cat"="application/vnd.ms-pki.seccat"
".cdf"="application/x-netcdf"
".cdr"="application/x-cdr"
".cel"="application/x-cel"
".cer"="application/x-x509-ca-cert"
".cg4"="application/x-g4"
".cgm"="application/x-cgm"
".cit"="application/x-cit"
.doc application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.rtf application/rtf
.xls application/vnd.ms-excel application/x-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.ppt application/vnd.ms-powerpoint
.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.pps application/vnd.ms-powerpoint
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pdf application/pdf
.swf application/x-shockwave-flash
.dll application/x-msdownload
.exe application/octet-stream
.msi application/octet-stream
.chm application/octet-stream
.cab application/octet-stream
.ocx application/octet-stream
.rar application/octet-stream
.tar application/x-tar
.tgz application/x-compressed
.zip application/x-zip-compressed
.z application/x-compress
.wav audio/wav
.wma audio/x-ms-wma
.wmv video/x-ms-wmv
.mp3 .mp2 .mpe .mpeg .mpg audio/mpeg
.rm application/vnd.rn-realmedia
.mid .midi .rmi audio/mid
.bmp image/bmp
.gif image/gif
.png image/png
.tif .tiff image/tiff
.jpe .jpeg .jpg image/jpeg
.txt text/plain
.xml text/xml
.html text/html
.css text/css
.js text/javascript
.mht .mhtml message/rfc822
四牧牢、responseType
"" responseType 設(shè)為空字符串與設(shè)置為"text"相同,默認(rèn)類(lèi)型
"text" 返回的是包含在 DOMString 對(duì)象中的文本姿锭。
"document" 返回的是一個(gè) HTML Document 或 XML XMLDocument
"arraybuffer" 返回的是一個(gè)包含二進(jìn)制數(shù)據(jù)的 JavaScript ArrayBuffer
"blob" 返回的是一個(gè)包含二進(jìn)制數(shù)據(jù)的 Blob 對(duì)象
"json" 返回的是一個(gè) JavaScript 對(duì)象 塔鳍。這個(gè)對(duì)象是通過(guò)將接收到的數(shù)據(jù)類(lèi)型視為 JSON 解析得到的。
"ms-stream" 返回的是下載流的一部分 呻此;此響應(yīng)類(lèi)型僅允許下載請(qǐng)求轮纫,并且僅受Internet Explorer支持
版權(quán)聲明:本文為CSDN博主「多啦阿貓的阿貓」的原創(chuàng)文章
原文鏈接:https://blog.csdn.net/weixin_45184157/article/details/125807890