el-upload 上传表单验证

摘要:
˂els inputtype=“text
<template>
  <div>
    <div class="container">
      <el-form
        style="60%;"
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="描述" prop="desc">
          <el-input type="textarea" maxlength="50" show-word-limit v-model="form.desc"></el-input>
        </el-form-item>

        <el-form-item label="添加图片" prop="file" ref="uploadElement">
          <el-upload
            action="#"
            multiple
            v-model="form.file"
            ref="upload"
            list-type="picture-card"
            :auto-upload="false"
            :on-preview="showImg"
            :on-remove="remove"
            :on-change="onChange"
            class="upload-demo"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>

        <el-form-item style="margin-top: 50px;">
          <el-button type="primary" @click="onSubmit('form')">立即添加</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "userInfo",
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      formData: "",
      form: {
        desc: "",
        file: ""
      },
      rules: {
        file: [{ required: true, message: "请上传图片", trigger: "change" }]
      }
    };
  },
  methods: {
    //添加信息
    onSubmit(form) {
      let that = this;
      this.$refs[form].validate(valid => {
        if (valid) {
          let formData = new FormData();
          for (var key in that.form) {
            formData.append(key, that.form[key]);
            console.log(formData.get(key));
          }
          that.formData = formData;
          // this.addForm();
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    //重置信息
    resetForm(form) {
      this.$refs[form].resetFields();
      this.form.file = "";
      this.$refs.upload.clearFiles();
    },
    //添加
    addForm() {
      this.$axios({})
        .then(res => {
          console.log(res.data.status);
          if (res.data.status == "200") {
            this.$message({
              message: "添加成功",
              type: "success"
            });
            this.$router.push({
              path: "/"
            });
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //删除图片
    remove(file) {
      // console.log(file);
      for (let i = 0; i < this.form.file.length; i++) {
        if (file === this.form.file[i]) {
          this.form.file.splice(i, 1);
        }
      }
    },
    //变化设置图片
    onChange(file, fileList) {
      // console.log(file, fileList);
      this.form.file = fileList;
      console.log(this.form);
    },
    //浏览图片
    showImg(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
};
</script>

转自:https://blog.csdn.net/qq_33327325/article/details/103898709

免责声明:文章转载自《el-upload 上传表单验证》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Qt5之反射机制(内省)报错 Invalid options in vue.config.js: "baseUrl" is not allowed 问题解决下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue按键修饰符@keyup.enter.native

Vue 允许为 v-on 在监听键盘事件时添加按键修饰符: <!-- 只有在 `key` 是 `Enter` 时调用 `vm.submit()` --> <input v-on:keyup.enter="submit"> 应用场景: 1、当我们在登录页面中,输完密码后,点击enter键就可以发起登录请求 <!-- 登录表单区...

form表单提交时选择性传值到后台

正常情况下form表单提交会把表单内的内容提交到后台,但是如果有些内容只是作为展示或者是标记而不想传到后台,我们采用如下方法: jsp页面如下,我们不想提交id为userIdMark和pwdMark的input框的值到后台,所以我们需要在提交时设置input框的属性为disabled, 这样当表单提交时,他们的值就无法提交到后台,从而达到目的 <fo...

【Android】图片(文件)上传的请求分析结构

  怎么在android中上传文件,即怎么用Java向服务器上传文件、上传图片,这是个老问题了,在网上能搜到现成的代码,很多朋友用起来也比较熟了,但是为什么这么写,可能很多朋友并不清楚,这篇文章就来分析一下Web中文件上传的请求包内容。如有问题请联系zhfch.class@gmail.com。   当然说,这篇文章被我冠了一个“Android”的前缀,因为...

深入剖析PHP输入流 php://input

另附一个一个连接: http://www.nowamagic.net/academy/detail/12220520 ///////////////////////////////////////////////////////////////另一种解释////////////////////////////////////////////////////...

Ajax提交表单数据(包含文件)

1. 表单数据->JSON->后台 2. 表单序列化【方式一】 jquery.serializejson.js <script src="http://t.zoukankan.com/js/jquery.serializejson.js"></script> <script> $('#btnRegi...

x-www-form-urlencoded与multipart/form-data区别

http://blog.chinaunix.net/uid-7210505-id-329700.html application/x-www-form-urlencoded 与 multipart/form-data 的区别 Submitted by ryan on Mon, 09/20/2010 - 00:25 在Flex中,UrlRequest中的...