vue 下载文件(后台返回为二进制流)

    技术2025-09-01  5

    将二进制流转换为需要的文件格式

    downloadContent(value, name) { const link = document.createElement('a') const blob = new Blob([value], { type: 'application/octet-stream' }) const url = window.URL.createObjectURL(blob) link.href = url link.download = name + '.xlsx' link.click() window.URL.revokeObjectURL(url) },

    需要在下载接口的请求头中加 responseType: ‘arraybuffer’,例:

    async function getDownThreatFile(params) { const res = request({ url: `firework/ioc/bath/export`, method: 'get', params, responseType: 'arraybuffer' }) return res }

    value:后台返回的二进制流 name:文件名 type:文件类型 具体参考文件类型

    Processed: 0.009, SQL: 9