前端二进制文件下载和二进制文件的上传

    // 下载模板
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组件返回的二进制文件

 

posted on 2024-01-25 16:07  ChoZ  阅读(49)  评论(0编辑  收藏  举报

导航