elementUI upload 上传文件 先选择文件 在上传服务器

 

vue data中的数据

data(){
  return {
            //文件数据
            file: "",
            filename: "",
            filelist: [],
            errmesg: [],
            btn: {
              disable: false,
              message: "开始上传",
             },
     }
}

页面代码

<el-upload
              style="min-height: 15vh"
              ref="upload"
              name="file"
              :limit="1"
              :auto-upload="false"
              accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-exceed="handleExceed"
              :file-list="filelist"
              :on-change="handleChansge"
            >
              <el-button slot="trigger" size="small" type="primary"
                >选择文件</el-button
              >
              <el-button
                style="margin-left: 10px"
                size="small"
                type="success"
                @click="postfile"
                :disabled="btn.disable"
                >{{ btn.message }}</el-button
              >
              <div
                slot="tip"
                class="el-upload__tip"
                style="margin-top: 20px; font-size: 14px"
              >
                上传文件只能为excel文件,且为xlsx,xls格式
              </div>
            </el-upload>
            <!--此处可以写导入时出现的详细错误信息,包含第几行出现问题显示出来-->
            <div
              v-for="(item,index) in errmesg"
              :key="'err'+index"
              class="text"
            >
              {{ item.message }}
            </div>

 

handleExceed(e) {
            // 判断是否只能上传一个文件,超过提示报错
            console.log(e);
            this.$notify.error({
              title: "错误",
              message: "只能上传一个文件哦",
            });
          },
          //判断上传的文件是否符合格式
          handleChansge(file, fileList) {
            let name = file.name;
            if (!/\.(xlsx|xls|XLSX|XLS)$/.test(file.name)) {
              this.$notify.error({
                title: "错误",
                message: "上传文件只能为excel文件,且为xlsx,xls格式",
              });
              this.filelist = [];
              this.file = "";
              return false;
            }
            this.file = file.raw;
            this.filename = file.name;
          },
          //调用后台接口 导入表格数据
          postfile() {
            let that = this;
            if (this.file == "") {
              that.$notify.error({
                title: "错误",
                message: "上传文件不能为空",
              });
              return false;
            } else {
              // 文件形式的需要用 formData.append() 传递参数
              let formData = new FormData();
              formData.append("file", this.file);
              formData.append("publicTransportationCompanies", this.busCompanyValue);
              formData.append("carFactory",this.carFactoryValue);
              let config = {
                headers: { "Content-Type": "multipart/form-data" },
              };
              this.btn.disable = true;
              this.btn.message = "上传中,请等待";
               //这里是 我的接口 你们可根据自己的接口更换即可 
              api.post("/import/importInfo", formData, config)
                .then(function (response) {
                  console.log(response);
                  if (response.data.code == 200) {
                    that.$notify({
                      title: "上传成功",
                      message: response.msg,
                      type: "success",
                    });
                    that.filelist = [];
                    that.btn.disable = false;
                    that.btn.message = "上传服务器";
                    that.centerDialogVisible = true;
                  } else if (response.data.code == 500) {
                    //这里是我的判断 可以忽略不计 你们根据自己接口返回 做相应操作
                    that.errmesg = response.data.data.map((item) => {
                      let str = item.orderNo
                        ? "订单号: " + item.orderNo
                        : item.companyName
                        ? "所属组织: " + item.companyName
                        : item.plateNo
                        ? "车牌号: " + item.plateNo
                        : item.termNo
                        ? "控住器ID: " + item.termNo
                        : item.carNo
                        ? "车辆自编号:" + item.carNo
                        : item.roulesName
                        ? "所属线路批次:" + item.roulesName
                        : item.busType
                        ? "车辆型号:" + item.busType
                        : item.templateName
                        ? "轮胎模板名称: " + item.templateName
                        : item.carFactory
                        ? "车厂:" + item.carFactory
                        : item.publicTransportationCompanies
                        ? "公交公司:" + item.publicTransportationCompanies
                        : "";
                      return {
                        message:
                          "第" + item.number + "行 --> " + str + " " + item.reason,
                      };
                    });
                  }
                })
                .catch((err) => {
                  that.btn.disable = false;
                  that.btn.message = "上传服务器";
                  that.$notify.error({
                    title: "错误",
                    message: "上传过程出错啦",
                  });
                });
            }
          },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值