后台返回的乱码流

解决办法:

请求方式用的是axios,主要加关键的 {responseType: 'blob'}

axios封装

export function postDownload(url, data) {
  return new Promise((resolve, reject) => {
    instance.post(url,data,{responseType: 'blob'}).then(response => {
      resolve(response);
    }, err => {
      reject(err)
    })
  })
}

下载插件 npm install js-file-download -S

运用:

下载excel时,后台设置了excel标题,要去请求头去取,传输过程中文会有乱码的情况,需要编码下。

let fileDownload = require("js-file-download");

fileDownload(res.data,decodeURIComponent(res.headers['content-disposition'].split("=")[1]));

 

 

基于element下载

<el-upload
    class="upload-file"
    ref="upload"                                
    action="https://jsonplaceholder.typicode.com/posts/"
    :limit="1"
    :before-upload="beforeupload"
    :auto-upload="false">
</el-upload>
beforeupload(file) {
    let params = new FormData()
    let data= {
        params.append('file',file)
    }
}

 

转载于:https://www.cnblogs.com/adbg/p/10579408.html

Logo

前往低代码交流专区

更多推荐