el-upload自定义上传文件,并携带其余参数

493 阅读1分钟

el-upload自定义上传文件,并携带其余参数

懒得编辑,比较简单看代码吧。有帮助的点个赞呗

  <!-- 上传附件 -->
    <e-dialog
      :dialogTitle="dialogTitle"
      :centerDialogVisible="dialogVisiblel"
      @updateVisible="resetPopupData"
      @resetPopupData="resetPopupData"
      @submitPopupData="submitPopupData"
      :popupWidth="'560px'"
      :destroyClose="true"
    >
          .......
          <el-upload
            ref="uploadFile"
            action
            :http-request="getUploadFile"
            :on-change="handleUploadChange"
            :on-preview="handlePreview"
            :auto-upload="false"
            :headers="headers"
            accept=".xls,.xlsx"
          >
            <el-button
              type="primary"
              class="upload-btn"
              style="margin-left: 10px"
              >预览</el-button
            >
          </el-upload>
           ......
    // 上传文件
    async getUploadFile(val) {
      const file = new FormData();
      file.append("supplierCompanyId",this.listTypeInfo.supplierCompanyIdList[0]?.id);
      file.append("tenantCode", this.queryData.tenantCode);
      file.append("purchaseCompanyId", Number(this.$route.query.companyId));
      file.append("file", val.file);
      let res = await api.uploadProductPool(file);
      if (res.code == "200") {
        this.formData.fileName = "";
        let msg = this.dialogTitle == "导入新增" ? "导入文件成功" : "批量改价成功";
        this.$message.success(msg);
        this.dialogVisiblel = false;
      }
    },
    
    // 提交
    submitPopupData() {
      if (this.formData.fileName) {
        this.$refs.uploadFile.submit();
      } else {
        this.$message.warning("请先上传附件");
      }
    },
    
    // 上传之前校验
    handleUploadChange(file) {
      let typeArr = [".xlsx", ".xls"];
      if (typeArr.indexOf(file.name) != -1) {
        this.$message.warning("上传附件失败,仅可上传.xlsx .xls文件");
        return false;
      }
      this.formData.fileName = file.name;
    },