element-ui 表单+ 文件上传

2 篇文章 0 订阅
1 篇文章 0 订阅

el-form 包住 el-upload 并使用 rule 验证文件选择。

分解一下步骤
1、一个form标签 包括属性:rulesmodelref
2、一个 el-form-item 标签 包括属性 : prop
3、一个 el-upload 组件 在基础用法之上增加使用两个钩子函数(on-change on-remove

html结构如下:

<template>
  <div id="app">

    <el-form :rules="rule" :model="form" ref="form">
      <el-form-item prop="fileName">
        <el-upload
          ref="upload"
          :action="upload.action"
          :auto-upload="upload.autoUpload"
          :on-change="handleFileChange"
          :on-remove="handleFileRemove"
        >
          <el-button slot="trigger" type="primary">选择文件</el-button>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="handleSubmit()">开始上传</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  data() {
    return {
      // * form
      rule: { fileName: [{ required: true }] },
      form: {
        fileName: undefined
      },
      // * upload
      upload: {
        autoUpload: false,
        action: "",
        accept: ""
      }
    };
  },

el-upload 中的name 属性和rule 中的属性的可以不相同
关键在于
el-form-item 中绑定的键名proprule 要验证的属性名相同(建立关联)。
②使用文件上传组件的钩子函数on-changeon-remove手动修改el-form-item 绑定的值(验证对象)。

js

  methods: {
    /** 触发选择文件 */
    handleFileChange(file, fileList) {
      // ! 添加文件 手动添加this.form.uploadName 使其被rules捕捉为非空
      this.$set(this.form, "fileName", file);
    },
    /** 触发移除文件 */
    handleFileRemove(file, fileList) {
      // ! 移除文件 同时移除this.form.uploadName 使其被rules捕捉为空
      if (!fileList.length) this.$set(this.form, "fileName", undefined);
    },

    /** 触发提交 */
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitUpload();
        }
      });
    },
    submitUpload() {
      this.$refs.upload.submit()
      console.log("submit");
    },
  }

解析

1、rule 和 提交的form 对象建立关联之后,直接点击提交按钮将会验证到form.fileName 为空。
2、使用upload的钩子函数,手动的填充和清空form.fileName使rule判断结果发生改变。这个demo没有限制文件上传的数量,但是只要能改变rule的判断结果,就能控制是否进入验证成功后的提交阶段。当上传文件列表的文件被全部移除时,也将验证对象清空。文件列表可以通过钩子函数的参数fileList获取。
在这里插入图片描述

以上。

Element Plus 是一个基于 Vue 3 的组件库,其中包含了丰富的 UI 组件,包括文件上传组件 `ElUpload`。通过使用 `ElUpload` 组件,用户可以方便地上传文件,并且还可以与表单一起提交文件数据。 在 Element Plus 中,`ElUpload` 组件提供了两种文件上传模式:`button` 和 `drag`。你可以通过设置 `action` 属性来指定上传的服务器地址。当用户选择文件后,可以通过事件来获取这些文件,并将它们添加表单数据中进行提交。 以下是使用 Element Plus 的 `ElUpload` 组件进行文件上传后提交表单的基本步骤: 1. 在 Vue 组件中引入 `ElUpload` 组件。 2. 设置 `ElUpload` 的 `action` 属性为你的上传接口地址。 3. 使用 `before-upload` 钩子函数来处理文件上传前的逻辑,比如校验文件类型和大小。 4. 使用 `on-success` 或 `on-error` 钩子函数来处理文件上传成功或失败后的逻辑。 5. 在 `ElForm` 表单添加 `ElUpload` 组件,并确保表单有正确的提交机制,比如监听表单的提交事件,并在事件处理函数中处理上传后的逻辑。 6. 在表单提交时,`ElUpload` 会自动将已上传的文件列表作为表单数据的一部分一起发送到服务器。 示例代码如下: ```html <template> <el-form ref="form" @submit.native.prevent="handleSubmit"> <el-upload action="你的上传接口地址" :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" list-type="picture"> <el-button size="small" type="primary">选取文件</el-button> </el-upload> <el-form-item> <el-button type="primary" native-type="submit">提交</el-button> </el-form-item> </el-form> </template> <script> export default { methods: { handleSubmit() { // 获取表单引用并调用提交方法 this.$refs.form.validate((valid) => { if (valid) { // 表单验证通过后,可以在这里发送请求将数据提交到服务器 // 由于 Element Plus 会自动处理文件上传,所以这里的 data 应该是表单中剩余的数据 const formData = new FormData(); // 添加其他表单数据到 formData 中... // 最后,使用 axios 或者原生 fetch 发送请求 } }); }, beforeUpload(file) { // 文件上传前的逻辑 // 返回 false 可以阻止上传 return true; }, handleSuccess(response, file, fileList) { // 文件上传成功的处理逻辑 console.log('上传成功', response, file, fileList); }, handleError(err, file, fileList) { // 文件上传失败的处理逻辑 console.error('上传失败', err, file, fileList); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值