element plus + el-upload,上传多张图片

实现自定义下载、删除、查看

在这里插入图片描述

<template>
  <el-dialog :close-on-click-modal="false" :model-value="dialogVisibleBox" :before-close="handleCloseUploadImg">
    <el-upload
        ref="uploadRef"
        v-model:file-list="fileList"
        list-type="picture-card"
        :on-change="onChangeFun"
        :auto-upload="false"
        action="#">
      <el-icon>
        <Plus/>
      </el-icon>
      <template #file="{ file }">
        <div>
          <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
          <span class="el-upload-list__item-actions">
            <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
            >
              <el-icon><zoom-in /></el-icon>
            </span>
            <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
            >
              <el-icon><Download /></el-icon>
            </span>
            <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemoveFun(file)"
            >
              <el-icon><Delete /></el-icon>
            </span>
          </span>
        </div>
      </template>
    </el-upload>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloseUploadImg">取消</el-button>
        <el-button type="primary" @click="saveData">
          确认
        </el-button>
      </span>
    </template>
    <el-dialog v-model="dialogVisible">
      <img w-full :src="dialogImageUrl" alt="Preview Image"/>
    </el-dialog>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, toRefs, watch } from 'vue';
import {ElNotification, UploadProps, UploadUserFile} from 'element-plus';
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue';
const props = defineProps({
  dialogVisibleBox: Boolean
});
let {dialogVisibleBox} = toRefs(props);
const uploadRef: any = ref();
let fileList: any = reactive<UploadUserFile[]>([
  // {
  //   : 'food.jpeg',
  //   url: '../../../assets/image/order/blue_light_2.png',
  // }
]);
const saveImgList: any = {};
const dialogImageUrl = ref('');
let disabled = ref(false);
const dialogVisible = ref(false);
let saveUploadImgNum: number = 0;

const onChangeFun = (rawFile: any) => {
  if (rawFile.status !== "ready") return;
  const maxSize = 30;
  const imgSize = rawFile.size / 1024 / 1024 < maxSize;
  const imgType = ['image/png', 'image/jpg', 'image/jpeg'].includes(rawFile.raw.type);
  if (!imgType)
    ElNotification({
      title: '温馨提示',
      message: '请上传png、jpg、jpeg文件格式的图片!',
      type: 'warning',
    });
  if (!imgSize)
    ElNotification({
      title: '温馨提示',
      message: `上传图片大小不能超过 ${maxSize}M!`,
      type: 'warning',
    });
  if (imgType && imgSize) {
    saveUploadImgNum++;
  } else {
    // 当判定不是规定格式 删除
  	const index = fileList.indexOf(rawFile);
    if (index !== -1) { 
      fileList.splice(index, 1);
    }
  }
};
// 传递关闭事件
const emit = defineEmits(['handleCloseUploadImg']);
const handleCloseUploadImg = () => {
  // 清空upload列表
  uploadRef.value.clearFiles();
  emit("handleCloseUploadImg");
};
const handleRemoveFun= async (file: any) => {
  // 这里可以先处理后端删除 前端再删除
  const index = fileList.indexOf(file);
  if (index !== -1) { // 确保文件存在于文件列表中
     saveUploadImgNum--;
     fileList.splice(index, 1); // 从文件列表中删除文件
   }
}

const handlePictureCardPreview = (file: any) => {
  // 方法查看
  dialogImageUrl.value = file.url!;
  dialogVisible.value = true;
}

const handleDownload = (file: any) => {
  // 下载
  const link: any = document.createElement('a');
  link.download = file.name;
  link.href = file.url;
  link.click();
  window.URL.revokeObjectURL(link.href);
}
</script>
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值