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

vue实现文件上传,前后端

前端封装el-upload组件,父组件传值dialogVisible(用于显示el-dialog),子组件接收,并且关闭的时候返回一个值(用于隐藏el-dialog),最多上传五个文件,文件格式为.jpg\pdf\png

<template><div><el-dialog  width="30%"  :visible.sync="dialogShow" append-to-body @close='handleCancle' title="上传发票" class="uploadDialog"><!-- list-type="picture" --><el-uploadref="upload":auto-upload="false":http-request="uploadFile":on-change="changeFileLength":limit="5":on-exceed="handleExceed"action=""accept=".pdf,.jpg,.png" multiple><i class="el-icon-upload"></i><div class="el-upload__text">点击上传文件</div></el-upload><!-- 上传时点击的按钮 --><el-button @click="upload" type="success">上传文件</el-button></el-dialog></div>
</template>
<script>
import {  upload } from "@/api/invoice/invoiceManagement";export default {name: "uploadCT",props:{dialogVisible:{type:Boolean,default:false,require:true,}},watch: {dialogVisible: {handler(val) {this.dialogShow = val},deep: true, // 深度监听immediate: true, // 初次监听即执行  },},data(){return{// 上传文件的列表uploadFiles: [],// 上传文件的个数filesLength: 0,// 上传需要附带的信息info:{id:"",name:"",},//显示dialogShow:this.dialogVisible,}},methods:{//超出限制提示handleExceed(files, fileList) {this.$message.warning(`当前限制选择 5 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);},//关闭handleCancle(){this.uploadFiles= [];// 上传文件的个数this.filesLength= 0;this.dialogShow = false;this.$emit('closeUploadDialog',this.dialogShow);this.$refs.upload.clearFiles();},// 修改当前文件列表长度changeFileLength(file, fileList){this.filesLength = fileList.length},// 用户点击上传调用async upload(){// 触发上传 调用配置 :http-request="uploadFile"// 即触发 uploadFile函数await this.$refs.upload.submit();// 上传完成后执行的操作 ...this.$modal.msgSuccess("上传成功");},// 该函数还是会被调用多次// 每次param参数传入一个文件uploadFile(param){console.log("参数",param);// 将文件加入需要上传的文件列表this.uploadFiles.push(param.file)// 当uploadFiles长度等于用户需要上传的文件数时进行上传if (this.uploadFiles.length == this.filesLength){// 创建FormData上传let fd = new FormData()// 将全部文件添加至FormData中this.uploadFiles.forEach(file => {fd.append('file', file)})// 将附加信息添加至FormDatafd.append("id", this.info.id)fd.append("name", this.info.name)// 配置请求头const config = {headers: {"Content-Type": "multipart/form-data",}}console.log("参数",fd);// 上传文件upload(fd).then(res => {/*上传成功处理*/console.log(res);if(res.msg=='上传成功'){this.uploadFiles=[];this.filesLength = 0;this.dialogShow = false;this.$emit('closeUploadDialog',this.dialogShow);this.$refs.upload.clearFiles();}}).catch(err => {/*报错处理*/});}}}
}
</script>

后端接收

 @PostMapping("/upload")public AjaxResult upload(@RequestParam(value = "file") MultipartFile[] file){try {String localPath = "";//1.1获取当前日期,当做本地磁盘的目录Date nowDate = DateUtils.getNowDate();String format = new SimpleDateFormat("YYYYMMDD").format(nowDate);String localPathPrefix = "C:\\"+format;for(MultipartFile f:file){// 获取文件名String fileName = f.getOriginalFilename();// 获取文件后缀String prefix = fileName.substring(fileName.lastIndexOf("."));// 保存文件到本地磁盘localPath = localPathPrefix+"\\"+fileName;File localFile = new File(localPath);if (!localFile.getParentFile().exists()) {localFile.getParentFile().mkdirs();}//写入到本地磁盘f.transferTo(localFile);// 获取文件在本地磁盘上的路径String filePath = localFile.getAbsolutePath();log.info("文件名称:"+fileName+"已经存入本地磁盘,全路径为:"+filePath);//上传到文件服务器,自己掉接口//上传完成后,删除本地临时磁盘文件if (localFile.delete()) {log.info(localFile.getName() + "已经删除");} else {log.info("文件删除失败");}}//删除本次磁盘的日期目录File file1 = new File(localPathPrefix);if (file1.delete()) {log.info(file1.getName() + "已经删除");} else {log.info("文件删除失败");}}catch (Exception e){System.out.println(e);return error("上传失败");}return success("上传成功");}

效果展示

 

 

http://www.lryc.cn/news/129530.html

相关文章:

  • OJ练习第151题——克隆图
  • keepalived+lvs实现高可用
  • 【Let‘s make it big】英语合集61~70
  • python实现图像的二分类
  • 8.深浅拷贝和异常处理
  • Element Plus el-table 数据为空时自定义内容【默认为 No Data】
  • 使用nginx和frp实现高效内网穿透:简单配置,畅通无阻
  • Python土力学与基础工程计算.PDF-螺旋板载荷试验
  • 低代码开发ERP:精打细算,聚焦核心投入
  • 顺序表(数据结构)
  • stable_diffusion_webui docker环境配置
  • 【Java】常见面试题:HTTP/HTTPS、Servlet、Cookie、Linux和JVM
  • 批量爬虫采集完成任务
  • intelij idea 2023 创建java web项目
  • 【论文笔记】基于指令回译的语言模型自对齐-MetaAI
  • MySQL和MariaDB的版本对应关系
  • Python数据的输入与输出
  • 生成国密密钥对
  • ASR(自动语音识别)任务中的LLM(大语言模型)
  • 简单介绍一下centos上有什么工具可以优雅的管理开机启动项
  • 万宾燃气管网监测解决方案,守护城市生命线安全
  • Django框架 靓号管理(增删改查)
  • 责任链模式简单实现
  • Excel自动化办公——Openpyxl的基本使用
  • 解决Fastjson2 oom(Out Of Memory),支持大对象(LargeObject 1G)json操作
  • SpringBoot + redis处理购物车逻辑
  • open cv学习 (五) 图像的阈值处理
  • NVIDIA vGPU License许可服务器高可用全套部署秘籍
  • 基于CNN卷积神经网络的口罩检测识别系统matlab仿真
  • 《HeadFirst设计模式(第二版)》第九章代码——迭代器模式