element 上传图片

1. element 的上传图片

复制代码
<template>
  <el-upload
      ref="imgUpload"
      :before-upload="(file)=>beforeAvatarSuccess(file,'xslx')"
      :http-request="onRequest"
      action=""
      list-type="picture-card">
    <i slot="default" class="el-icon-plus"></i>
    <div slot="file" slot-scope="{file}">
      <img
          :src="file.url"
          alt="" class="el-upload-list__item-thumbnail"
      >
      <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
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file,'imgUpload')"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
    </div>
  </el-upload>
  <el-dialog :visible.sync="dialogVisible">
    <img :src="dialogImageUrl" alt="" width="100%">
  </el-dialog>
</template>
<script>

export default {
  name: 'About',
  data() {
    return {
      isImages: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imageList: {
        xslx: []
      }
    }
  },
  methods: {
    beforeAvatarSuccess(file, id) {
      const isJPG =
          file.type === 'image/jpg' || file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('只能上传png,jpg,jpeg类型的图片')
      }
      if (!isLt2M) {
        this.$message.error('上传图片不能超过2M')
      }
      this.isImages = id
      return isLt2M && isJPG
    },
    //上传图片
    onRequest(params) {
      let formdata = new FormData()
      formdata.append('file', params.file)
      uploadImgs(formdata).then(res => {
        if (res.status) {
          this.$message.success('上传成功')
          this.imagesList[this.isImages].push(res.data)
        }
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    //删除图片
    handleRemove(file, ref) {
      let uploadFiles = this.$refs[ref].uploadFiles;
      for (var i = 0; i < uploadFiles.length; i++) {
        if (uploadFiles[i]['url'] == file.url) {
          uploadFiles.splice(i, 1)
        }
      }
    },
    handleDownload(file) {
      window.open(file,url)
    }
  }
}

</script>
复制代码

 

posted @   小成-  阅读(116)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示