vue将后端返回的二进制流前端进行处理实现下载文件

【vue】将后端返回的二进制流前端进行处理实现下载文件

因为后端返回的是这样的二进制流,也没有返回状态码,所以会被拦截,需要在request.js文件对返回的状态码进行处理

因为后端返回的是这样的二进制流,也没有返回状态码,所以会被拦截,需要在request.js文件对返回的状态码进行处理

image.png

requests.js里面修改下

// 响应时拦截
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('用户信息已过期,请重新登录', '系统提示', {
          confirmButtonText: '登录',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          store.dispatch('user/resetToken').then(() => {
            //清空sessionStorage
            clearStorage();
            //清空token过期时间
            removeTokenTime();
            location.reload()
          })
        })
      } else if (!res.code) { // 如果没有返回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过期时间
    removeTokenTime();
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

Content-Type部分媒体类型

".*"="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 设为空字符串与设置为"text"相同,默认类型
"text"  返回的是包含在 DOMString 对象中的文本。
"document"  返回的是一个 HTML Document 或 XML XMLDocument
"arraybuffer"  返回的是一个包含二进制数据的 JavaScript ArrayBuffer 
"blob"  返回的是一个包含二进制数据的 Blob 对象
"json"  返回的是一个 JavaScript 对象 。这个对象是通过将接收到的数据类型视为 JSON 解析得到的。
"ms-stream"  返回的是下载流的一部分 ;此响应类型仅允许下载请求,并且仅受Internet Explorer支持

版权声明:本文为CSDN博主「多啦阿猫的阿猫」的原创文章

原文链接:https://blog.csdn.net/weixin_45184157/article/details/125807890

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。