vue+axios 前端下载文件-解决后端返回的文件流乱码问题

打印出后端返回的数据:乱码了 前端

解决方法:

在前端请求的时候携带请求头responseType:blob,ios

axios({
        method: 'GET',
        url: '/api',
        params: params,
        responseType: 'blob'
    }).then(res=>{
        console.log(res)
        let blob = new Blob([res.data], {type: "application/vnd.ms-excel"});
        let url = window.URL.createObjectURL(blob);
        window.location.href = url;
    }).catch(err=>{
        console.log(err)
    })
 
复制代码

携带请求头后,再次打印后端返回的数据,data为Blob对象了, axios

👍【注意】:后端

axios使用封装事后的请求设置请求头responseType:'blob'不生效,推荐你们使用axios({})的方式,不要使用如:axios.get()的方式api

相关文章
相关标签/搜索