使用Element-UI中的el-upload实现文件的上传demo(亲测有用)

简介: 使用Element-UI中的el-upload实现文件的上传demo(亲测有用)


先介绍一个demo ,前端框架使用的是VUE,组价用的是el-upload组件,它的相关介绍在官方文档中有介绍,点击即可跳转Upload 上传 ,那么话不多少,上代码。


前端代码如下


<template>
  <el-dialog  class="dialog" v-model="dialogFormVisible" title="导入文件" width="30%" align-center>
   <el-form :model="form" >
     <el-form-item label="导入数据文件" :label-width="formLabelWidth">
       <el-upload
         v-model:file-list="fileList"
         class="upload-demo"
         action="#"
         multiple
         :auto-upload="false"
         :on-change="handleChange"
         :on-remove="handleRemove"
         :limit="2"
     >
     <el-button type="primary">Click to upload</el-button>
     </el-upload>
     </el-form-item>
   </el-form>
   <template #footer>
     <span class="dialog-footer">
       <el-button @click="dialogFormVisible = false">Cancel</el-button>
       <el-button type="primary" @click="confirm">
         Confirm
       </el-button>
     </span>
   </template>
 </el-dialog>
</template>
<script>
import {excelToolImport} from "@/utils/post";
export default {
 data () {
   return {
     dialogFormVisible:false,
     fileMap:{},
     form:{
       name: '',
       region: '',
       date1: '',
       date2: '',
       delivery: false,
       type: [],
       resource: '',
       desc: '',
     },
     fileList:[]
   }
 },
 components: {},
 methods: {
   changeVisible(){
     this.dialogFormVisible = !this.dialogFormVisible
   },
   handleChange(file, fileList) {
     this.fileList = fileList
   },
   handleRemove(file, fileList) { 
     this.fileList = fileList
   },
   async confirm(){
     let self =this
     debugger
     let formData = new window.FormData();
     for(let i=0;i<this.fileList.length;i++){
       formData.append('file', this.fileList[i].raw);
     }
     console.log(formData.get('file'))
     excelToolImport(formData)
     self.dialogFormVisible = false
   },
 },
 watch: {},
 created () {},
 mounted () {}
}
</script>
<style scoped>
.dialog{
 color: #124373;
 background: linear-gradient(0deg, rgba(101,186,246,0.2), rgba(0,0,0,0));
}
.comAddBatch-row-file {
   font-size: 0.9vw;
   width: 325px;
   color: #fff;
   flex: 1;
   max-width: calc(100% - 80px);
   height: 22px;
   display: flex;
   align-items: center;
   border: 1px solid #1197f3;
   box-shadow: 0 0 7px 1px rgba(33, 152, 235, 0.3) inset;
   border-radius: 5px;
 }
 .file-name {
   flex: 1;
   padding-left: 15px;
   white-space: nowrap;
   overflow: hidden;
   text-overflow: ellipsis;
 }
 .second-row{
   display: flex;
   height: 22px;
 }
 .font{
   height: 19px;
   font-size: 14px;
   font-family: Microsoft YaHei;
   font-weight: 400;
   color: #1b9cba;
   line-height: 16px;
 }
 .comAddBatch-line {
 width: 100%;
 height: 1px;
 opacity: 0.1;
 border-bottom: 1px solid #57b6fa;
 margin: vw(10) auto;
}
.comAddBatch-row {
 width: 100%;
 height: 52px;
 display: flex;
 align-items: center;
 padding: 0 1vw;
 font-size: 0.9vw;
 color: #1883d0;
 font-family: "Microsoft YaHei";
 }
.comAddBatch-row-title {
   margin-top: 10px;
 }
.comAddBatch-file-blue {
color: #1e71eb;
}
.comAddBatch-file-green {
 color: #00b163;
}
.comAddBatch-file-red {
 color: #f74f66;
}
.comAddBatch-file-num {
 margin: 0 0.5vw;
 font-family: Impact;
 font-size: 26px;
}
</style


对应的js文件如下


import axios from 'axios'
// 测试 EXCEL数据导入ES
export function excelToolImport (obj) {
  return axios.post( '/tool-station/testExcelTool',obj
)
}


对应的后端接口


@RequestMapping(value = "/testExcelTool", method=RequestMethod.POST)
    public String excelImport(@RequestParam(value = "file") MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            InputStream in = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            Path path1 = Files.createFile(Paths.get("data/" + originalFilename));
            File file1 = new File(String.valueOf(path1));
            FileOutputStream os = new FileOutputStream(file1);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) != -1) {
                os.write(buffer, 0, length);
            }
            os.flush();
            in.close();
            os.close();
        }
相关文章
|
6天前
|
Web App开发 JSON JavaScript
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
31 0
|
7月前
|
Web App开发 资源调度 JavaScript
SAP Fiori Elements 应用里的 ui5.yaml 文件详解试读版
SAP Fiori Elements 应用里的 ui5.yaml 文件详解试读版
79 0
|
6天前
|
Web App开发 前端开发 JavaScript
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
34 1
|
6天前
|
移动开发 开发框架 JavaScript
什么是 SAP UI5 项目 ui5.yaml 文件中的 specVersion 字段
什么是 SAP UI5 项目 ui5.yaml 文件中的 specVersion 字段
19 0
|
6天前
|
UED
SAP UI5 开发项目 package.json 文件里的 @sap/ux-specification 依赖
SAP UI5 开发项目 package.json 文件里的 @sap/ux-specification 依赖
25 0
|
7月前
|
JSON 数据格式 开发者
SAP UI5 manifest.json 文件里 extends 区域的内容是如何被解析的
SAP UI5 manifest.json 文件里 extends 区域的内容是如何被解析的
38 1
|
7月前
|
XML JavaScript 前端开发
SAP UI5 Fiori Elements annotation 文件序列化成 DOM 对象的逻辑
SAP UI5 Fiori Elements annotation 文件序列化成 DOM 对象的逻辑
31 0
|
7月前
|
XML 缓存 前端开发
SAP UI5 应用启动时加载的 library-preload.js 文件
SAP UI5 应用启动时加载的 library-preload.js 文件
25 0
|
7月前
|
开发者
SAP UI5 应用启动时加载的 flpSandbox.html 文件有什么作用
SAP UI5 应用启动时加载的 flpSandbox.html 文件有什么作用
22 0
|
7月前
|
存储 移动开发 JSON
SAP UI5 manifest.json 文件的 config 区域
SAP UI5 manifest.json 文件的 config 区域
55 0

热门文章

最新文章