vue中后台返回格式为二进制流进行文件的下载

结合项目中

封装get请求携带token,进行接收二进制流

export function getHeader() {
    const token = getToken();
    if (token) {
        return {
            headers: {
                "Authorization": "Bearer " + token,
            }
        }
    }
    return {
        headers: {}
    }
}

export function getHeader() {
    const token = getToken();
    if (token) {
        return {
            headers: {
                "Authorization": "Bearer " + token,
            }
        }
    }
    return {
        headers: {}
    }
}

具体文件使用:

TaskManageServer.js文件:

export const dataExport2  = (vueObject, dataIdList) =>  getDataRaw(vueObject,dataExport1(dataIdList))

export const dataExport1 = (dataIdList)=> {
    var tmp ="";
    for (let i = 0; i 

对应html文件具体使用:

 import {
        dataExport1,
        dataExport2
    } from './TaskManageServer'

 

const response = await dataExport2(this, this.dataIdList);
let blob = new Blob([response.data], {
    //下载的文件类型;
    type: 'application/zip' 
})
// let fileName = Date.parse(new Date()) + '.zip'(修改下载的文件名)
if (window.navigator.msSaveOrOpenBlob) {
    // navigator.msSaveBlob(blob, fileName)
    navigator.msSaveBlob(blob)
} else {
    var link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    // link.download = fileName
    link.click()
    window.URL.revokeObjectURL(link.href) //释放内存
}

 以上即可实现下载二进制流文件。

 

 

你可能感兴趣的:(vue中后台返回格式为二进制流进行文件的下载)