前端二进制文件下载和二进制文件的上传
// 下载模板 export function downTemplate(data = {}) { return request({ url: `${apiPrefix}/system/user/importTemplate`, method: 'POST', data, responseType: 'blob' // 需要添加返回类型 }) } const res = await downTemplate() if (res.status === 200) { let blob = new Blob([res.data], { //res.data为返回的文件流 type: 'application/xls;charset=UTF-8' }) let fileName = '导入模板.xls' if ('download' in document.createElement('a')) { // 非IE下载 let a = document.createElement('a') a.download = fileName a.style.display = 'none' a.href = URL.createObjectURL(blob) document.body.appendChild(a) a.click() URL.revokeObjectURL(a.href) // 释放URL 对象 document.body.removeChild(a) } else { // IE10+下载 navigator.msSaveBlob(blob, fileName) } }
const formData = new FormData() formData.append('file', file) //file是upload组件返回的二进制文件
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步