js下载文件流,提示文件损坏问题
js下载文件流,提示文件损坏问题
前端axios,也需要加responseType: “blob”,这样才能正常下载文件流。
eg:
axios({ url: layui.setter.apiHost + '/api/order/putAllPages', method: "POST", headers: { Authorization: "Bearer " + layui.storage.getToken(), AppKey: layui.setter.TenantKey, TenantKey: layui.setter.TenantKey }, timeout: 50000, data: Qs.stringify({ type: _this.type, userType: 'buyer', querySubOrganization: subOrgan.querySubOrganization, organId: subOrgan.organId, startTime: startTime, endTime: endTime }), responseType: 'blob' }).then(function (res) { layer.closeAll('loading'); var blob = new Blob([res.data], { type: "application/vnd.ms-excel" }) var url = URL.createObjectURL(blob); var a = document.createElement("a"); var fileName = '订单核算.xls' a.download = fileName; a.style.display = "none" a.href = url; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url) })
Lee2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步