当前位置: 首页 > article >正文

el-upload照片墙二次封装

         基于el-upload文件上传组件的二次封装。

         使用场景一般是提交表单中的附属信息,组件要实现的是图片的上传,还有图片的回显。那么现在给出组件代码。

<template>
  <div>
    <el-upload
      action=""
      :http-request="customRequest"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :file-list="fileList"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog
      title="图片详情"
      :visible.sync="dialogVisible"
      append-to-body
      top="56px"
    >
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
  
<script>
import { fileUpload, fileDownload } from "@/api/system/common";
export default {
  props: {
    onUploadSuccess: {
      type: Function,
      required: true,
    },
    initialUrls: {
      type: Array,
      default: () => [],
    },
    init: {
      type: Boolean,
      require: true,
    },
  },
  data() {
    return {
      action: "",
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [],
    };
  },
  watch: {
    init() {
      if (!this.init) {
        // 关闭对话框时,重置
        this.fileList = [];
      }
    },
  },
  methods: {
    async updateFileList() {
      const promises = this.initialUrls.map(async (item) => {
        const response = await fileDownload({ path: item.filePath });
        item.url = URL.createObjectURL(new Blob([response.data]));
        return item; // 返回更新后的 item
      });

      // 等待所有的 Promise 完成
      this.fileList = await Promise.all(promises);
    },
    beforeUpload(file) {
      // 限制最多上传三个文件
      if (this.fileList.length >= 3) {
        this.$message.error("最多只能上传三个图片");
        return false; // 返回 false 以阻止上传
      }
      return true; // 允许上传
    },
    customRequest({ file, onSuccess, onError }) {
      const formData = new FormData();
      formData.append("file", file);

      fileUpload(formData)
        .then((response) => {
          return response;
        })
        .then((res) => {
          onSuccess(res); // 调用成功回调
        })
        .catch((error) => {
          onError(error); // 调用错误回调
        });
    },
    handleRemove(file, fileList) {
      // 从 fileList 中移除被删除的文件
      this.fileList = fileList;
      this.onUploadSuccess(this.fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleUploadSuccess(response, file, fileList) {
      const imageUrl = response.url || file.url; // 根据实际情况获取 URL
      this.fileList.push({
        name: file.name,
        url: imageUrl, // 将新上传的图片 URL 添加到列表中
        filePath: response.data.filePath,
      });

      this.onUploadSuccess(this.fileList);
    },
  },
};
</script>

       使用了 Element UI 库中的 el-upload 和 el-dialog 组件,用于实现图片上传和预览功能。

父组件调用

初始化时,调用照片墙组件的回显方法

        这里要注意的是el-upload中回显和上传使用的file-list列表必须要满足它的格式,不然无法回显图片,所以我们存储的时候用一个json格式字段去保存,在前端去解析和转换。

数据库


http://www.kler.cn/news/355299.html

相关文章:

  • golang 报错:invalid character ‘‘ looking for beginning of object key string
  • Mobile ALOHA 简介
  • 数据库管理-第252期 深入浅出多主多活数据库技术- Cantian存储引擎(二)(20241017)
  • K8s简介和安装部署
  • 嵌入式开发学习日记——数据结构基础
  • 算法——python实现堆排序
  • leetcode 47.全排列||
  • Flink简介及小案例
  • SpringBoot框架下购物推荐网站的设计模式与实现
  • 网络资源模板--Android Studio 实现简易新闻App
  • 10.15.2024刷华为OD C题型(二)
  • 怎么一键下载网页所有图片?3个方法轻松搞定
  • 论文笔记:D-vlog 用于抑郁症检测的多模态数据集
  • 智慧园区能带来哪些便利?
  • 基于SpringBoot+Vue+uniapp微信小程序的婚庆摄影小程序的详细设计和实现(源码+lw+部署文档+讲解等)
  • CentOS 7- 配置阿里镜像源
  • HTML_文本标签
  • MySQL【知识改变命运】05
  • 计数型信号量
  • 【C语言】函数指针