随笔 - 752  文章 - 0 评论 - 33 阅读 - 134万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

后台输出流到前端

//有错误信息,就导出文件
            if (errorList != null && errorList.size() > 0) {
                String title = "导入国产药数据的异常数据";
                ExcelUtils.exportExcel(errorList, title, title, StandardCode.class, title, response);
                return null;
            }

在响应拦截器中加以下代码:

if (headers['content-type'].toLowerCase() === 'application/vnd.ms-excel;charset=utf-8') {
          return response
      }

如果不加这段代码会报错:

No converter for [class com.ljxx.entity.Result] with preset Content-Type 'application/vnd.ms-excel;charset=UTF-8'

由于上述代码返回的是response,故使用filedownload下载的时候应该为response.data

复制代码
handleUploadChange1(file) {
      if (file.name.lastIndexOf('.') < 0) {
        this.$message.error('上传文件只能是xls、xlsx格式!')
        return
      }
      const testMsg = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
      const extensionXLS = testMsg == 'xls'
      const extensionXLSX = testMsg == 'xlsx'
      if (!extensionXLS && !extensionXLSX) {
        this.$message.error('上传文件只能是xls、xlsx格式!')
        return
      }
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传文件不能超过 2MB!')
        return
      }
      console.log('import continue')
      this.importLoading = true
      this.importDisabled = true
      const data = new FormData()
      data.append('file', file.raw)
      standardCode.importDomesticExcel(data).then(response => {
        console.log(response)
        if (response.success) {
          this.open2(response.msg)
          this.importLoading = false
          this.importDisabled = false
          this.getList()
        } else {
          this.$message.success("部分数据导入失败,数据已下载到本地,请查看!")
          fileDownload(response.data, '导入数据中的异常数据.xlsx')
          // this.open2(response.msg)
          this.importLoading = false
          this.importDisabled = false
        }
      }).catch(() => {
        this.open2('抱歉,导入失败')
        this.importLoading = false
        this.importDisabled = false
      })
    },
复制代码

 如果报错文件打不开,如下:

 是因为responseType值应为arraybuffer,而不能是arraybuffer;charset=utf-8

复制代码
importDomesticExcel(data) {
    return request({
      url: '/standardCode/import',
      method: 'post',
      data: data,
      responseType: 'arraybuffer',
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
复制代码

其中:multipart/form-data:指定传输数据为二进制类型,比如图片、mp3、文件。



感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接
posted on   周文豪  阅读(2541)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示