导出文件时,后台返回文件流,前端如何接收并下载?

647 阅读1分钟

需要在封装接口的地方加入:

 responseType: 'blob', //重要!

image.png

下图是具体业务处理方法:

image.png

    exportPl({
        storeId: this.$route.params.id
    }).then(res => {
        let fileName = res.headers['content-disposition'].split('=')[1]
        // 获取文件名
        let objectUrl = URL.createObjectURL(new Blob([res.data]))
        // 文件地址
        const link = document.createElement('a')
        link.download = fileName
        link.href = objectUrl
        link.click()
    }).catch(e => {

    })