zdppy_api+vue3+antd实现批量上传文件的功能

前端代码版本1

在这个版本中,能够实现文件上传以后,通过文件列表的链接点击以后进行回显。

但是有个问题,那就是文件的状态一直是加载中。

html 复制代码
<template>
  <a-upload
      action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
      :multiple="true"
      :file-list="fileList"
      @change="handleChange"
  >
    <a-button>
      <upload-outlined></upload-outlined>
      Upload
    </a-button>
  </a-upload>
</template>
<script setup>
import {ref} from 'vue';
import {UploadOutlined} from "@ant-design/icons-vue";

const fileList = ref([
  {
    uid: '-1',
    name: '1.jpg',
    status: 'done',
    url: 'http://127.0.0.1:8888/download/1.jpg',
  },
]);

const handleChange = info => {
  let resFileList = [...info.fileList];

  // 1. Limit the number of uploaded files
  //    Only to show two recent uploaded files, and old ones will be replaced by the new
  resFileList = resFileList.slice(-2);

  // 2. read from response and show file link
  resFileList = resFileList.map(file => {
    if (file.response) {
      // Component will show file.url as link
      file.url = file.response.url;
    }
    return file;
  });
  fileList.value = resFileList;
};
</script>

前端代码版本2

这个版本中,简化了前端的代码。

在文件上传成功以后,我们提取上传文件的唯一标识,追加到了要回显的文件列表中。

html 复制代码
<template>
  <a-upload
      :multiple="true"
      :file-list="fileList"
      :customRequest="customRequest"
  >
    <a-button>
      <upload-outlined></upload-outlined>
      Upload
    </a-button>
  </a-upload>
</template>
<script setup>
import {ref} from 'vue';
import {UploadOutlined} from "@ant-design/icons-vue";
import axios from "axios";

const fileList = ref([
  {
    uid: '-1',
    name: '1.jpg',
    status: 'done',
    url: 'http://127.0.0.1:8888/download/1.jpg',
  },
]);

const customRequest = (option) => {
  const formData = new FormData();
  const fileUrl = "http://127.0.0.1:8888/upload";
  formData.append('file[]', option.file);
  axios.postForm(fileUrl, {
    file: option.file,
  }).then(res => {
    console.log(res)
    const data = res.data.data
    console.log("data xxxxxxxxxx", data)
    fileList.value.push({
      uid: data.uuid,
      name: data.file_name,
      status: 'done',
      url: 'http://127.0.0.1:8888/download/' + data.file_name,
    })
  })
}
</script>
相关推荐
s石有八九11 分钟前
PDF/文档LLM作业批改:2025年研究现状、技术进展与研究空白
人工智能·python·pdf·教育·ai教育·作业批改
岱宗夫up21 分钟前
基于ROS的视觉导航系统实战:黑线循迹+激光笔跟随双模态实现(冰达机器人Nano改造)
linux·python·机器人·ros
Y_0339 分钟前
SpringBoot+VUE3的图书管理系统
vue.js·spring boot·毕业设计·数据可视化
别或许1 小时前
python中的异步调用(直接使用教程)
java·前端·python
百***24371 小时前
DeepSeek-V3.2全解析:开源大模型的性能巅峰与落地实践指南
python·开源
xkxnq1 小时前
第四阶段:Vue 进阶与生态整合(第 47 天)(Vue 项目目录结构解析:每个文件夹的作用与规范)
前端·javascript·vue.js
方安乐1 小时前
杂记:Quart和Flask比较
后端·python·flask
嫂子开门我是_我哥1 小时前
第十六节:异常处理:让程序在报错中稳定运行
开发语言·python
测试19981 小时前
如何使用Appium实现移动端UI自动化测试?
自动化测试·软件测试·python·测试工具·ui·appium·测试用例
yuankoudaodaokou1 小时前
无图纸如何定制汽车外饰件?3DeVOK MT+ QUICKSURFACE逆向设计解决方案
python·3d·汽车·机器翻译