el-upload手动上传图片并限制图片数量、大小和格式

news/2024/7/10 2:07:46 标签: vue, elementui

template部分:

  <!-- 修改弹窗 -->
  <el-dialog :title="dialogTxt" @close="closeDialog" :visible.sync="alertBox">
    <el-form ref="addForm" :rules="rules" :model="addForm" size="medium" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="知识标题:" prop="knowledgeTitle">
            <el-input v-model="addForm.knowledgeTitle"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="内容:" prop="knowledgeContent">
            <el-input v-model="addForm.knowledgeContent"></el-input>
          </el-form-item>
        </el-col>

        <!-- 图片上传 -->
        <el-col :span="12">
          <el-form-item label="图片选择:">
            <el-upload
              action="#"
              ref="uploadimg"
              :limit="1"
              :auto-upload="false"
              :on-change="imageChange"
              :show-file-list="true"
              :http-request="httpRequest"
              :file-list="fileList"
              list-type="picture-card"
              :on-exceed="handleExceed"
              :before-remove="handleRemove"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="knowledgeStatus">
            <el-select v-model="addForm.knowledgeStatus" placeholder="请选择" @change="selectStatus">
              <el-option label="可用" value="0"></el-option>
              <el-option label="已禁止" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <div class="submitBtn">
          <el-button type="primary" icon="el-icon-check" @click="uploadSucess('addForm')">提交</el-button>
          <el-button type="danger" icon="el-icon-circle-close" @click="cancleUpload">取消</el-button>
        </div>
      </el-row>
    </el-form>
  </el-dialog>

methods部分:

    //关闭弹框清空校验信息
    closeDialog() {
      this.$refs.expertForm.clearValidate();
    },
    //选择图片后做图片格式限制(手动上传图片时,before-upload钩子无效,使用此方法替代)
    imageChange(file, fileList) {
      const isImage =
        file.raw.type == "image/png" ||
        file.raw.type == "image/jpg" ||
        file.raw.type == "image/jpeg";
      const isLt5M = file.size < 1024 * 1024 * 5;
      if (!isImage) {
        this.$message.error("上传只能是png,jpg,jpeg格式!");
      }
      if (!isLt5M) {
        this.$message.error("上传图片大小不能超过 5MB!");
      }

      if (isImage && isLt5M) {
        this.uploadFile = file.raw || null;
      } else {
        fileList.splice(-1, 1);
      }
    },

    //超出限制个数钩子
    handleExceed(files) {
      this.$message.warning("当前限制选择1个文件");
    },
    // 删除图片
    handleRemove(file, fileList) {
      console.log(file);
      fileList.splice(0, 1);
      this.fileList = fileList;
      this.uploadFile = null;
      console.log("3333333", this.fileList);
      this.changeUrl = file.url;
    },

    httpRequest(params) {
      //解决删除文件时报错
      const prom = new Promise((resolve, reject) => {});
      prom.abort = () => {};
      return prom;
    },
    selectStatus() {
      this.ifStatus = true;
    },
    //  上传提交
    uploadSucess(formName) {
      this.$refs.uploadimg.submit();
      let formData = new FormData();
      if (this.addText == "add") {
        if (!this.uploadFile) {
          this.$message.error("图片不能为空!");
        } else {
          formData.append("file", this.uploadFile);
          formData.append("title", this.addForm.knowledgeTitle);
          formData.append("content", this.addForm.knowledgeContent);
          formData.append("status", this.addForm.knowledgeStatus);
          this.$refs[formName].validate(valid => {
            if (valid) {
              this.$store
                .dispatch("baseSet/uploadSucess", formData)
                .then(() => {
                  this.knowledgeManager(this.pageNum);
                  this.alertBox = false;
                  this.$refs.uploadimg.clearFiles();
                  this.ifStatus = false;
                });
            }
          });
        }
      } else {
        if (!this.uploadFile) {
          this.$message.error("图片不能为空!");
        } else {
          if (this.uploadFile) {
            formData.append("file", this.uploadFile);
          }

          formData.append("title", this.addForm.knowledgeTitle);
          formData.append("content", this.addForm.knowledgeContent);
          formData.append("status", this.addForm.knowledgeStatus);
          formData.append("id", this.changeId);
          this.$refs[formName].validate(valid => {
            if (valid) {
              this.$store
                .dispatch("baseSet/changeKnowledge", formData)
                .then(() => {
                  this.knowledgeManager(this.pageNum);
                  this.alertBox = false;
                  this.$refs.uploadimg.clearFiles();
                  this.ifStatus = false;
                });
            }
          });
        }
      }
    },
    cancleUpload() {
      this.alertBox = false;
      this.fileList = [];
    },


http://www.niftyadmin.cn/n/1065963.html

相关文章

vue跳转页面不刷新的问题

问题&#xff1a;跳转页面的时候&#xff0c;如果是之前打开过的页面&#xff0c;就会保持着上次跳转的状态不更新 原因&#xff1a;vue-router的切换不同于传统的页面切换&#xff0c;而是路由之间的切换&#xff0c;其实就是组件之间的切换&#xff0c;引用相同组件的时候&a…

Android中接口(Interface)的简单使用

Android中接口(Interface)的简单使用 Java中的接口可以被看作是只包含常量和抽象方法的抽象类 。可以使用如下方式定义一个接口&#xff1a; public interface InterfaceDemo {int i 10;void method1();int method2(); } 使用1&#xff1a; 解决“多重继承”的问题 Java语言本…

python实现削苹果小游戏

也不用998只有199源码发你。 支付完发我邮箱发你源代码。

蜗居的蚁族何不多蹦蹦,也许能成为跳蚤

先进行名词解释&#xff0c;估计大家也都知道是什么意思&#xff0c;不过还是先扫盲一下~~~ 蜗居&#xff1a;①比喻窄小的住所&#xff0c;常用作谦词&#xff1b;②伏处、潜居&#xff1b;③是指那些房奴因每月要还房贷&#xff0c;经济能力受限&#xff0c;从而生活行为受限…

sed 集合(项目中的笔记)

奇数行和偶数行合并为一行&#xff1a; Like: Sequence number: 5398Sequence name: Glyma.16G123500.1Sequence number: 5399Sequence name: Glyma.16G169500.1Sequence number: 5400Sequence name: Glyma.16G113000.1Sequence number: 5401Sequence name: Glyma.16G176800.1S…

微信公众号 scanQRCode:fail, the permission value is offline verifying

1、公众号后台白名单 查看公众号后台配置有没有设置正确的安全域名和白名单等 2、确认config正确通过 3、如果是在页面加载好时就调用了JSAPI&#xff0c;则必须写在wx.ready的回调中 4、查看jsApiList是否添加了相应JSAPI router.beforeEach((to, from, next) > {// 获…

算法学习

1、数据结构对问题很重要。 2、基本过程&#xff1a;初始算法&#xff0c;分析效率&#xff0c;改进&#xff0c;继续迭代&#xff0c;直到满意。 3、并查集算法&#xff0c;及应用转载于:https://www.cnblogs.com/alan215m/p/5793728.html

dhl:报错:LINQ to Entities 不支持指定的类型成员“Date”

Linq如&#xff1a; var v from l in _dal.Share where l.PingcoId pingcoId && (l.CreateTime.Date DateTime.Now.Date) select l; return v.ToList(); 会报错&#xff1a;LINQ to Entities 不支持指定的类型成员“Date”。 改成这样OK&#xff1a; Dat…