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++;
  }
};
// 传递关闭事件
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>

相关推荐

  1. vue2 upload图片

    2024-01-10 15:50:05       17 阅读
  2. uniapp 图片到django后端

    2024-01-10 15:50:05       35 阅读
  3. uni-app选择图片并压缩——2024.04.02

    2024-01-10 15:50:05       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 15:50:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 15:50:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 15:50:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 15:50:05       20 阅读

热门阅读

  1. 帕金森病的原因是什么?

    2024-01-10 15:50:05       40 阅读
  2. Linux进阶命令使用

    2024-01-10 15:50:05       38 阅读
  3. 计算机网络专栏目录

    2024-01-10 15:50:05       43 阅读
  4. C++ 字符串哈希 || 字符串前缀哈希法

    2024-01-10 15:50:05       30 阅读
  5. Android studio ListView应用设计

    2024-01-10 15:50:05       32 阅读
  6. Flutter GetX 之 状态管理

    2024-01-10 15:50:05       37 阅读
  7. 如何彻底卸除Microsoft Edge浏览器

    2024-01-10 15:50:05       37 阅读
  8. LeetCode每日一题 | 2707. 字符串中的额外字符

    2024-01-10 15:50:05       45 阅读