当前位置: 首页 > article >正文

vue之element-ui文件上传(二)

一、点击上传,使用默认的action上传,添加校验,上传成功后,去除校验:

<el-form-item label="文件md5" prop="fileMd5">
          <el-upload
            v-if="!form.fileMd5"
            v-model="form.fileMd5"
            class="upload-demo"
            :action="uploadUrl"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :before-remove="beforeRemove"
            accept=".apk"
            :limit="1"
            :headers="headers"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary" icon="el-icon-upload"
              >点击上传</el-button
            >
            <div slot="tip" class="el-upload__tip">只能上传apk文件</div>
          </el-upload>
          <span v-if="form.fileMd5">{{ form.fileMd5 }}</span>
  </el-form-item>

js:

data() {
    return {
      
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        fileMd5: [
          { required: true, message: "请上传APK文件", trigger: "blur" },
        ],
        type: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个分析类型",
            trigger: "change",
          },
        ],
      },
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      uploadUrl:
        process.env.VUE_APP_BASE_API + `${config.service}/main/apkImport`,
      fileList: [],
      fileData: null,
    };
  },
 methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.form.fileMd5 = "";
      this.validateField("fileMd5");
    },
    handlePreview(file) {
      console.log(file);
    },
    // 文件个数超出
    handleExceed(files, fileList) {
      this.$message.warning(`上传文件数量不能超过1个!`);
    },
    beforeRemove(file, fileList) {
      this.form.fileMd5 = "";
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    handleSuccess(response, file, fileList) {
      console.log("上传成功信息", response, file, fileList);
      // const { fileName = "", url = "" } = response.data;
      // this.fileList.push({ name: file.name, url });
      this.$message.success("上传成功!");
      this.handleInputClear("fileMd5");
      this.fileData = JSON.parse(response.data);
      this.form.fileMd5 = JSON.parse(response.data).fileMd5;
    },
    handleInputClear(val) {
      this.$refs.form.clearValidate(val);
    },
    validateField(val) {
      this.$refs.form.validateField(val);
    },
    
  },

二、拖拽上传,和表单一起通过formData提交,添加校验,上传成功后,去除校验:

<el-form-item label="文件上传" prop="file">
          <el-upload
            class="upload-demo"
            v-model="form.file"
            drag
            action="#"
            accept=".xls,.xlsx"
            :auto-upload="false"
            :limit="1"
            :before-remove="beforeRemove"
            :on-remove="handleRemove"
            :on-exceed="handleExceed"
            :on-success="handleSuccess"
            :on-change="handleChange"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处,或<em>点击上传</em>
            </div>
            <!-- <div class="el-upload__tip" slot="tip">只能上传.xlsx文件</div> -->
          </el-upload>
 </el-form-item>

js:

 data() {
    return {
      
      // 表单参数
      form: {},
      rules: {
        taskName: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
        ],
        categoryId: [
          { required: true, message: "请选择分类", trigger: "change" },
        ],
        monitoringDeadline: [
          {
            required: true,
            message: "请选择日期时间",
            trigger: "change",
          },
        ],
        file: [{ required: true, message: "请上传Excel文件", trigger: "blur" }],
      },
     
      
    };
  },

methods: {
    
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    handleRemove(file, fileList) {
      this.form.file = "";
      this.$refs.form.validateField("file");
    },
    // 文件个数超出
    handleExceed(file, fileList) {
      this.$message.warning(`上传文件数量不能超过1个!`);
    },
    handleSuccess(response, file, fileList) {
      console.log("上传成功信息", response, file, fileList);
      this.$message.success("上传成功!");
      this.$refs.form.clearValidate("file");
    },
    handleChange(file) {
      // console.log(file, 3333);
      this.form.file = file.raw;
      this.$message.success("上传成功!");
      this.$refs.form.clearValidate("file");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let obj = this.form;
          let formData = new FormData();
          for (const key in obj) {
            formData.append([key], obj[key]);
          }

          addMain(formData).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    
  },


http://www.kler.cn/a/473095.html

相关文章:

  • DAY15 神经网络的参数和变量
  • VSCode Live Server 插件安装和使用
  • 计算机网络 笔记 物理层
  • 游戏关卡设计的常用模式
  • HarmonyOS开发:传参方式
  • WPF中RenderTargetBitmap问题解决
  • Blockly 二次封装
  • 沁恒CH32V208GBU6定时器:开启定时器读取RSSI并且定时器单位为广播间隔单位一样;自动重装载定时器与关闭定时器
  • Elasticsearch(三)
  • 解决SSH连接时遇到的“远程主机身份验证已更改 (WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!)”警告
  • 八万字Java面试高频题目汇总(冲刺春招!)
  • Lua语言的函数实现
  • Objective-C语言的文件操作
  • wireshark抓包工具新手使用教程
  • .NET Core + Kafka 开发指南
  • MySQL 数据库分片技术指南
  • 数据库中锁与ETL的故障排除和性能优化
  • 【微服务】8、分布式事务 ( XA 和 AT )
  • Perl语言的文件操作
  • DeviceNet转Profinet网关如何革新污水处理行业!
  • tomcat12启动流程源码分析
  • adb使用及常用命令
  • JavaEE之定时器及自我实现
  • 闲谭SpringBoot--ShardingSphere分库分表探究
  • spring mvc源码学习笔记之一
  • Java高频面试之SE-10