vue 上传 excell文件 blob responseType: “arraybuffer“

11 篇文章 1 订阅

带excell后缀校验,效果如下
在这里插入图片描述

 <!-- Excel上传 -->
    <el-dialog title="文件上传"
     :visible.sync="excelVisible" width="40%" center>
  
      <el-upload style="text-align:center"
      :show-file-list="false"
      :on-error="handleError"
      :on-success="handleSuccess"
      :before-upload="handleBeforeUpload"
        class="upload-demo"
        drag
        :action="upUrl"
        multiple
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          仅限xls和xlsx文件
        </div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button style="margin-right:20px" size="mini" @click="excelVisible = false">取 消</el-button>
         <el-link target="_blank" :href="downUrl">
            <el-button size="mini" type="success" 
            >下载模板</el-button >
         </el-link>
       
      </span>
    </el-dialog>

data

	upUrl : url.upUrl,
      downUrl : url.downUrl,

methods

 handleBeforeUpload(file){
      // 校验
      let legalName = ['xlsx','xls']
      // 拿到后缀名
       let  name = file.name.substring(file.name.lastIndexOf('.') + 1,file.name.length)
       if(legalName.includes(name)){
         console.log(legalName.includes(name));
       }else{
         this.$message.info('文件格式不对,仅限xls和xlsx')
          return false
       }
        
      
    },
    handleError(err){
      // console.log(err);
       this.$message.info(err.data)
    },
     handleSuccess(response){
      //  console.log(response);
       if(response.isSuccess){
          this.$message.success(response.error)
          this.getListByParam()
          return
       }
       this.$message.info(response.error)
       
     },

如果上传后端返回文件流,但elementUi自带的上传不能设置responseType: “arraybuffer”,只能自己写;

在这里插入图片描述

uploadSectionFile(params){
       
        let form = new FormData()
        form.append('file',params.file)
        this.axios.post(`/busFbx/getGoodsCategory`,form,{responseType: "arraybuffer"})
        .then((res) => {
           let a = document.createElement("a");
          let blob = new Blob([res.data]);
          let objectUrl = URL.createObjectURL(blob);
          a.setAttribute("href", objectUrl);
          a.setAttribute("download", "分类模板.xls");
          a.click();
          // console.log(objectUrl);
          URL.revokeObjectURL(objectUrl); // 释放内存
    
        }) .catch((res) => {
        console.log(res);
        });
        
    },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值