vue下载excel文件,后台传过来的是文件流解决办法

在这里插入图片描述

 systemLog(Params)
          .then((res) => {
            let blob = new Blob([res], {type: "application/vnd.ms-excel"}); // res就是接口返回的文件流了
            let objectUrl = URL.createObjectURL(blob);
            console.log(objectUrl)
            const elink = document.createElement("a");
            elink.download = '下载'; //下载文件名称,
            elink.style.display = "none";
            elink.href = objectUrl;
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
          })
          .catch((error) => {
            console.log(error);
          });
      },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
首先,在 Vue 中使用 Upload 组件上传 Excel 文件需要在页面中引入 ElementUI 组件库。然后,可以按照以下步骤进行操作: 1. 在 Vue 组件中引入 Upload 组件: ```javascript <template> <el-upload class="upload-excel" action="/api/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :file-list="fileList" :accept=".xlsx, .xls" :auto-upload="false" > <el-button slot="trigger" size="small" type="primary">上传 Excel 文件</el-button> <div slot="tip" class="el-upload__tip">只能上传 .xlsx 或 .xls 格式的文件</div> </el-upload> </template> ``` 2. 在 Vue 组件中定义对应的 data 数据: ```javascript export default { data() { return { fileList: [] } }, methods: { handleSuccess(response, file, fileList) { // 上传成功后的处理逻辑 }, beforeUpload(file) { // 上传前的校验逻辑,如文件大小、文件类型等 } } } ``` 3. 在 Vue 组件中编写上传成功后的处理逻辑和上传前的校验逻辑: ```javascript handleSuccess(response, file, fileList) { this.$message({ message: '上传成功', type: 'success' }) // 处理成功后的逻辑 }, beforeUpload(file) { const isExcel = file.type === 'application/vnd.ms-excel' const isXlsx = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' const isLt10M = file.size / 1024 / 1024 < 10 if (!isExcel && !isXlsx) { this.$message.error('只能上传 .xlsx 或 .xls 格式的文件') return false } if (!isLt10M) { this.$message.error('上传文件大小不能超过 10MB') return false } return true } ``` 4. 在后台编写对应的接口处理上传的 Excel 文件。可以使用如下示例代码解析上传的 Excel 文件: ```python import pandas as pd def handle_upload_excel(request): file = request.files['file'] df = pd.read_excel(file) # 处理解析后的数据 return jsonify({'code': 0, 'message': '上传成功'}) ``` 以上就是在 Vue 中使用 Upload 组件上传 Excel 文件并请求后台接口的基本程,需要根据具体的业务逻辑进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sunny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值