vue手动上传excel

手动上传excel和上传图片类似,只是格式不一样
html:

<el-upload
          class="upload-demo inline-block"
          action="string"//随便填
          size="mini"
          accept=".xls, .xlsx"//限制上传类型
          :http-request="httpRequest"
          :file-list="showExcelist"//上传文件list
          :before-upload="beforeAvatarUpload"
        >
        <el-button size="small">选择文件</el-button>
</el-upload>
<el-button size="mini" type="primary" @click="saveexcel">保存</el-button>

js:

//上传前的操作,获取到上传excel的file
httpRequest(params) {
      this.excelFile = params;
      let formdata = new FormData();
      formdata.append("File", params.file);
      formdata.append("FileName", params.file.name);
},
beforeAvatarUpload(file) {
	//格式过滤
      const isXls =
        file.type === "application/vnd.ms-excel"
          ? true
          : file.type ===
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          ? true
          : false;
      if (!isXls) {
        this.$message.error("上传的文件只能是xls以及xlsx格式!");
      } else {
        this.excelFile = file;//获取到file 单独存一下
      }
      return isXls;
    },
//走接口,传给后端
saveexcel() {
      if (this.excelFile.file) {
        let formdata = new FormData();//传参格式手动改为fromdata
        let params = this.excelFile;
        formdata.append("batchFile", params.file);
        formdata.append("FileName", params.file.name);
        importSaledetail(formdata).then(res => {//请求后端,自己封装的axios
          if (res.rtnCode == 200) {//成功
            this.$notify({
              title: "成功",
              message: res.msg,
              type: "success"
            });
            this.excelFile = []; //成功后参数置空
            this.showExcelist = [];//成功后上传文件list置空
          } else  {//失败
            this.$message.error(res.msg);
          }
        });
     } else {
        this.$message.error("请选择文件!");
     }
},
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值