首页 > 其他分享 >vue+element 上传文件及文件夹

vue+element 上传文件及文件夹

时间:2023-12-21 18:55:32浏览次数:28  
标签:files vue form parent formData element loading 文件夹 上传

有需求需要做一个上传压缩包及文件夹的需求,记性不好,记录一下。

HTML 使用的element的upload和form表单,dropdown下拉菜单

<div>
<el-col :span="24">
<el-form-item label="材料路径:" prop="fileName">
<el-col :span="21">
<el-input v-model="form.fileName" :readonly="true"></el-input>
</el-col>
<el-col :span="3" align="center">
<el-dropdown trigger="click">
<span class="el-dropdown-link">
<i class="iconfont iconshangchuan upload-icon"></i>
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>
<div @click="emptyData">
<el-upload
class="project-upload"
ref="upload"
action=""
:multiple="false"
:auto-upload="false"
:on-change="handleChange"
:show-file-list="false"
accept=".skp, .3ds, .zip, .rar"
>
上传文件
</el-upload>
</div>
</el-dropdown-item>
<el-dropdown-item>
<div style="cursor: pointer" @click="emptyData">
<span class="upload-span">上传文件夹</span>
<input
title="上传"
class="upload-folder-input"
multiple=""
webkitdirectory=""
accept="*/*"
type="file"
@change="getFiles"
/>
</div>
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</el-col>
</el-form-item>
</el-col>
<div class="uptypestyleclass">请上传后缀名为.zip或者文件夹</div>
</div>

<div class="btn-up-group-box">
<el-button class="active" @click="handleUpload('concessionFormRef')">确定</el-button>
<el-button @click="cancelUpdateplan">取消</el-button>
</div>

JS

// 全局定义变量
let currentfile = []

// 清空存放文件的数组及输入框文字
// this.form.fileName = '' form表单
// this.files data中定义

emptyData() {
currentfile = []
this.form.fileName = ''
this.files = []
},
// 存放文件的数组
handleChange(file, fileList) {
currentfile.push(file)
this.form.fileName = file.name
this.$set(this.form, 'fileName', file.name)
},

getFiles(e) {
// let formData = new FormData()
this.files = e.target.files || []
this.form.fileName = this.files.length
? this.files[0].webkitRelativePath.split('/')[0]
: ''
},


// 点击确定开始上传
// formName是判断使用哪一个表单数据
handleUpload(formName) {
let vm = this
this.$refs[formName].validate((valid) => {
if (valid) {
vm.loading = vm.$loading({
lock: true,
text: '正在上传...',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.3)',
})
var formData = new FormData()
if (this.dialogType !== 'add') {
this.form = {
id: this.form.solutionPlanId,
name: this.form.name,
}
}
if (currentfile.length !== 0) {
currentfile.forEach((res) => {
formData.append('files', res.raw) // 文件对象 supplyUploadFile
})
formData.append('fileType', 1)
}
if (this.files.length !== 0) {
for (let i = 0; i < this.files.length; i++) {
formData.append('files', this.files[i])
}
formData.append('fileType', 2)
}
for (const i in this.form) {
if (i !== 'files' && i !== 'fileName') {
formData.append(i, this.form[i])
}
}
// 判断是新增还是编辑
if (this.dialogType === 'add') {
// 后台新增接口
addMyPlan(formData)
.then((res) => {
vm.loading.close()
if (res.success) {
this.$message.success('新增成功')
this.$parent.$parent.getMyPlanTree()
this.$parent.$parent.dialogVisible = false
}
})
.catch((err) => {
vm.loading.close()
})
} else {
// 后台编辑接口
editMyPlan(formData)
.then((res) => {
vm.loading.close()
if (res.success) {
this.$message.success('编辑成功')
this.$parent.$parent.getMyPlanTree()
this.$parent.$parent.dialogVisible = false
}
})
.catch((err) => {
vm.loading.close()
})
}
} else {
return false
}
})
},

// 点击取消时清空 新增 编辑的取消有些不同 看情况修改

cancelUpdateplan() {
this.currentStep = ''
this.$parent.$parent.dialogVisible = false
currentfile.length = 0
},

CSS


// 部分样式 按照自己需求更改
.project-upload {
text-align: center;
.iconfont {
font-size: 20px;
color: #f8d71c;
}
}

.upload-folder-input {
width: 100%;
position: absolute;
left: 0;
line-height: 30px;
opacity: 0;
}

 

参考文章:http://blog.ncmem.com/wordpress/2023/12/21/vueelement-%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%e5%8f%8a%e6%96%87%e4%bb%b6%e5%a4%b9/

欢迎入群一起讨论

 

 

标签:files,vue,form,parent,formData,element,loading,文件夹,上传
From: https://www.cnblogs.com/songsu/p/17919866.html

相关文章

  • django+vue实现文件夹上传
    最近学django的文件上下传,网上的文件夹上下传压根没有,找了好几个,报错一大堆,没有一个能用,花里胡哨,可气!!!下面这个方法是我刚刚用过的,分享给大家。前端vue非常简单,template部分<inputtype="file"id="twos"webkitdirectory/><el-buttontype="primary"@click="sumfolder">文件夹......
  • vue3--使用ref获取Dom元素
    vue2中,ref使用:在div元素上绑定了ref属性,并命名为hello,接下来我们直接使用this.$refs.hello的方式就可以获取到该DOM元素了。<template><divid="app"><divref="hello">Vue2,ref获取dom元素</div></div></template><script>......
  • 基于vue3和elementplus实现的自定义table组件
    基于vue3和elementplus实现的自定义table组件,集成查询工具栏分页,可通过配置直接实现基础的列表页基于vue3和elementplus实现的自定义table组件,集成查询工具栏分页,可通过配置直接实现基础的列表页目录结构如下:类型声明:declaretypeDictType={value:string|boolean|n......
  • Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
    在WPF应用的开发过程中Binding是一个非常重要的部分。在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的。这里将实际中碰到过的问题做下汇总记录和理解。1. source= {binding}和source={bindingRelativeSource={RelativeSourceself},Path=Dat......
  • vue3 + xlsx 实现 excel 导入web页面解析成json数据
    vue3+xlsx实现excel导入web页面并解析成json数据fileIipt动态创建的标签,一定要用户点击事件触发,不然文件选择框的弹出会被拦截,无法弹出。意思就是下面这段关键代码要用一个事件区触发执行,不能主动执行(比如:vue的钩子)import*asXLSXfrom'xlsx'//v:"^0.18.5"letfil......
  • avue select多选 格式化列的内容
    AVUE  formatter用来格式化列内容formatter:(val,value,label)=>{letarr=val.invoiceType.split(',');letstr='';for(letindex=0;index<arr.length;index++){......
  • VUE专栏——1.Vue简介
    一、Vue是什么? 2.谁开发的? 3.Vue的特点      如果有一天数据发生变化呢   5.官网 ......
  • 一文教你Vue3中的useDialog,让你的代码更加优雅!
    在日常开发时,弹窗是一个经常使用的功能,而且重复性极高,你可能会遇到下面这些问题:1、一个页面内多个弹窗,要维护多套弹窗状态,看的眼花缭乱2、弹窗内容比较简单,声明变量+模板语法的方式写起来比较麻烦关于这些问题,我首先想到的是应该弄一个即用即走的Dialog,不用去单独维护它......
  • git 下载指定文件夹的方法
    有时候一个项目非常大,特别像一些课程源码往往只需要下载一个文件夹。下面是用git下载指定目录的办法。这里以 https://gitee.com/dotnetmoyu/SimpleAdmin.git为例,只下载其中的web文件夹。  git命令如下://克隆元数据到本地,但不开始下载gitclone-nhttps://gitee.......
  • element-ui中的el-table底部固定指定行
    1,固定一行合计的情况  https://element.eleme.cn/#/zh-CN/component/table 直接使用官方文档上的summary-method  2,固定指定行或者多行  使用样式去固定例子:(计算列表数据的平均值,最大值,最小值并固定底部)1,计算数据的值protectedcalcData(data:any){......