vue下载后台文件流为excl文件

1.post 请求
加 responseType: ‘blob’
axios.post(${url},params ,{headers: {‘Authorization’: Auth,‘Content-Type’: contentType,responseType: ‘blob’}})

 MemberInfoExport(this.formData).then(res => {
        if (res) {
          console.log(res)
          this.downloadFile(res)
        }
      })
 downloadFile (data) {
      let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
      let url = window.URL.createObjectURL(blob)
      const link = document.createElement('a') // 创建a标签
      link.href = url
      link.download = this.getNowFormatDate() // 重命名文件
      link.click()
      URL.revokeObjectURL(url) // 释放内存
    },
        downloadFile(data, name) {
      let blob = new Blob([data], { type: 'application/pdf' })//要指明type,不然下载不了
      let url = window.URL.createObjectURL(blob)
      const link = document.createElement('a') // 创建a标签
      link.href = url
      link.download = name // 重命名文件
      link.click()
      URL.revokeObjectURL(url) // 释放内存
    },
    getNowFormatDate () {
      var date = new Date()
      var seperator1 = '-'
      var seperator2 = '-'
      var month = date.getMonth() + 1
      var strDate = date.getDate()
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
      }
      var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + ' ' + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds()
      const name = '会员信息'
      currentdate = name + currentdate
      return currentdate
    }

2.get 请求
axios.get(${url}, {params: params,headers: {‘Authorization’: Auth,‘Content-Type’: ‘application/json’},responseType: ‘blob’})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在 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 文件并请求后台接口的基本流程,需要根据具体的业务逻辑进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值