element的el-upload实现多个图片上传以及预览与删除

<el-form-item
            label="实验室照片:"
            prop="labUrlList"
            v-if="ruleForm.labHave"
          >
            <el-upload
              :action="urlUpload"
              :headers="loadHeader"
              accept=".jpg,.jpeg,.png"
              list-type="picture-card"
              :file-list="ruleForm.labUrlList"
              class="labUrlClass"
              :on-success="handleImgSuccess"
              :before-upload="beforeAvatarUpload"
              multiple
            >
              <!-- <i slot="default" class="el-icon-plus"></i> -->
              <img src="../../assets/images/photoSmall.png" />
              <span class="el-upload__text">选择照片</span>

              <div slot="file" slot-scope="{ file }">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="(file.response||{}).data"
                  alt=""
                />
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in"></i>
                  </span>
                  <span
                    class="el-upload-list__item-delete"
                    @click="handlePictureCardRemove(file)"
                  >
                    <i class="el-icon-delete"></i>
                  </span>
                </span>
              </div>
            </el-upload>
            <el-dialog
              title="照片预览"
              :visible.sync="dialogVisible"
              class="imgDialog"
            >
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog>
          </el-form-item>
data(){
	return {
		ruleForm:{
				labUrlList:[]
			}	
		}
}

js

    handlePictureCardRemove(file, fileList) {
      console.log(file, this.ruleForm.labUrlList);
      const index = this.ruleForm.labUrlList.findIndex((item) => {
        return item.uid === file.uid
      })
      this.ruleForm.labUrlList.splice(index, 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleImgSuccess(val, e, el) {
       if (e.response.code != 0) {
        this.$message.error(e.response.msg);
        return false;
      }
      //文件上传成功,清空提示信息
      this.$refs.formOneRef.clearValidate();
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.$message.success("上传成功");
      }, 200);
      // this.fileList = el
      this.ruleForm.labUrlList = el;
      console.log(this.ruleForm.labUrlList, "this.fileList");
    },
    // 图片上传前的判断
    beforeAvatarUpload(file) {
      let imgType = ["jpg", "jpeg", "png"];
      let judge = false; // 后缀
      let type = file.name.split(".")[file.name.split(".").length - 1];
      for (let k = 0; k < imgType.length; k++) {
        if (imgType[k].toUpperCase() === type.toUpperCase()) {
          judge = true;
          break;
        }
      }
      // 验证图片格式
      if (!judge) {
        this.$message.error("图片格式只支持:JPG、JPEG、PNG");
        return false;
      }
      const isLt1M = file.size / 1024 / 1024;
      if (isLt1M > 1) {
        this.$message.error("上传头像图片大小不能超过1MB");
        return false;
      }
      return true;
    },

style

  /deep/.el-form-item__content {
    .labUrlClass {
      width: 470px !important;
      .el-upload.el-upload--picture-card {
        position: relative;
        .el-upload__text {
          position: absolute;
          bottom: -26px;
          left: 45px;
          color: rgba(153, 153, 153, 1);
        }
      }
    }
  }

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
el-upload 是一个基于 element-ui 的上传组件,可以实现上传文件的功能。如果要实现上传多张图片及修改的逻辑,可以按照以下步骤进行: 1. 在 el-upload 组件中设置 multiple 属性为 true,以支持多张图片的上传。 ```html <el-upload action="your-upload-api-url" :multiple="true" :default-file-list="defaultFileList" :on-success="handleUploadSuccess" > <el-button> 点击上传 </el-button> </el-upload> ``` 2. 在 data 中定义 defaultFileList 变量,用于存储上传成功后的图片列表。 ```javascript data() { return { defaultFileList: [] } }, ``` 3. 在上传成功的回调函数 handleUploadSuccess 中,将上传成功后的图片信息加入到 defaultFileList 中。 ```javascript handleUploadSuccess(response, file, fileList) { // 将上传成功后的图片信息加入到 defaultFileList 中 this.defaultFileList.push({ name: file.name, url: response.data.url }); }, ``` 4. 如果要实现修改图片的逻辑,可以在 el-upload 组件外添加一个图片预览组件,用于显示当前已上传的图片列表,并在点击某张图片时触发修改操作。 ```html <el-image v-for="(file, index) in defaultFileList" :key="index" :src="file.url" :preview-src-list="defaultFileList.map(item => item.url)" @click.native="handleImageClick(index)" > </el-image> ``` 5. 在 handleImageClick 方法中,根据点击的图片索引打开一个对话框或者弹出一个模态框,用于进行图片修改操作。 ```javascript handleImageClick(index) { // 打开对话框或弹出模态框,进行图片修改操作 // ... }, ``` 6. 在修改图片成功后,更新 defaultFileList 中对应图片的信息。 ```javascript handleImageEditSuccess(index, newFile) { // 更新 defaultFileList 中对应图片的信息 this.defaultFileList.splice(index, 1, { name: newFile.name, url: newFile.url }); }, ``` 以上是 el-upload 上传多张图片及修改的逻辑实现方法,需要根据具体场景进行相应的调整和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值