vue+el-upload實(shí)現(xiàn)多文件動態(tài)上傳
vue+el-upload多文件動態(tài)上傳,供大家參考,具體內(nèi)容如下
使用場景
點(diǎn)擊【添加/刪除】,可動態(tài)增加/刪除行數(shù),并為每行上傳附件,附件暫存前端,點(diǎn)擊【上傳】可以將所有附件和部分名稱同時(shí)提交后臺,實(shí)現(xiàn)表格的動態(tài)多文件上傳。其中el-upload ,相關(guān)鉤子函數(shù)可查看el-upload 官方文檔
這里的表格行的新增是在彈框中填完再寫入的,也可直接在表格中添加行,然后填寫內(nèi)容(template slot-scope=“scope” 注釋部分代碼),這里僅提供思路

代碼html部分
<div class="vue-box">
<div class="title-line">
其他必須持有的證照<el-button type="primary" size="mini" style="height: 20px;padding-top: 3px !important;margin-left: 50px" icon="el-icon-plus" @click="handleAddDetails">添加行</el-button>
<el-button type="primary" size="mini" style="height: 20px;padding-top: 3px !important;margin-left: 50px" icon="el-icon-plus" @click="doFileUpload()">上傳</el-button>
</div>
<el-table
:row-class-name="rowClassNameDeal"
:data="tableData"
style="width: 100%;">
<el-table-column
prop="id"
width="50"
label="序號">
</el-table-column>
<el-table-column
prop="cardName"
width="180"
label="證照名稱">
<!--<template slot-scope="scope">
<el-input size="mini" v-model="tableData[scope.row.id-1].cardName"></el-input>
</template>-->
</el-table-column>
<el-table-column
prop="cardNo"
width="180"
label="證件號碼">
<!--<template slot-scope="scope">
<el-input size="mini" v-model="tableData[scope.row.id-1].cardNo"></el-input>
</template>-->
</el-table-column>
<el-table-column
prop="startDate"
width="180"
label="起始日期">
<!--<template slot-scope="scope">
<el-date-picker
v-model="tableData[scope.row.id-1].startDate"
style="width: 80%;"
size="mini"
value-format="yyyy-MM-dd"
type="date"
placeholder="選擇日期">
</el-date-picker>
</template>-->
</el-table-column>
<el-table-column
prop="endDate"
width="180"
label="結(jié)束日期">
<!--<template slot-scope="scope">
<el-date-picker
v-model="tableData[scope.row.id-1].endDate"
style="width: 80%;"
size="mini"
value-format="yyyy-MM-dd"
type="date"
placeholder="選擇日期">
</el-date-picker>
</template>-->
</el-table-column>
<el-table-column
prop="address"
label="附件">
<template slot-scope="scope">
<el-upload
:ref="scope.row.cardName"
:http-request="dynamicUpload"
:before-upload="beforeUploadFile(scope.row)"
:on-remove="uploadRemove"
:before-remove="uploadBeforeRemove"
:on-preview="uploadPreview"
name="upload"
:limit="1"
:data="scope.row.cardName"
:on-exceed="uploadHandleExceed"
:file-list="tableData[scope.row.id-1].fileUploadedList">
<el-button size="mini" type="info">點(diǎn)擊上傳</el-button>
</el-upload>
</template>
</el-table-column>
<el-table-column
prop="date"
width="80"
label="操作">
<template slot-scope="scope">
<el-button size="mini" type="warning" @click="handleDeleteDetails(scope.row)">刪除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog title="證照信息" :visible.sync="addCardVisible" width="40%">
<el-form :model="fileInfo">
<el-form-item label="證照名稱" :label-width="labelWidth">
<el-input v-model="fileInfo.cardName" autocomplete="off" style="width: 100%;"></el-input>
</el-form-item>
<el-form-item label="證件號碼" :label-width="labelWidth">
<el-input v-model="fileInfo.cardNo" autocomplete="off" style="width: 100%;"></el-input>
</el-form-item>
<el-form-item label="生效日期" :label-width="labelWidth">
<el-date-picker type="date" placeholder="生效日期" value-format="yyyy-MM-dd" v-model="fileInfo.startDate" style="width: 100%;"></el-date-picker>
</el-form-item>
<el-form-item label="失效日期" :label-width="labelWidth">
<el-date-picker type="date" placeholder="失效日期" value-format="yyyy-MM-dd" v-model="fileInfo.endDate" style="width: 100%;"></el-date-picker>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="addCardVisible = false">取 消</el-button>
<el-button type="primary" @click="addFileDetail">確 定</el-button>
</div>
</el-dialog>
</div>
js部分代碼
let nodeVue = new Vue({
el: '.vue-box',
data: {
labelWidth: '150px',
tableData: [],
uploadParams:{
fileTagName: ''
},
totalFileList:[],
totalFileNameList:[],
addCardVisible:false,
fileInfo:{
cardName:'',
cardNo:'',
startDate:'',
endDate:''
}
},
methods:{
// 文件相關(guān)
uploadRemove:function(file) {
let that = this;
// 刪除文件列表中的文件
for(let i=0;i<that.totalFileNameList.length;i++){
if(that.totalFileNameList[i].fileName === file.name){
that.totalFileNameList.splice(i,1)
}
}
for(let i=0;i<that.totalFileList.length;i++){
if(that.totalFileList[i].name === file.name){
that.totalFileList.splice(i,1)
}
}
},
// 上傳文件參數(shù)設(shè)置
beforeUploadFile:function(row) {
console.log(row.cardName);
this.uploadParams.fileTagName=row.cardName
this.uploadParams.fid=row.id
},
// 下載文件,點(diǎn)擊文件列表中的文件下載
uploadPreview:function(file){
console.log(file);
},
uploadHandleExceed:function(files, fileList) {
this.$message.warning(`當(dāng)前限制選擇 1 個(gè)文件`);
},
uploadBeforeRemove:function(file) {
return this.$confirm(`確定移除 ${ file.name }?`);
},
// 附件添加行,打開添加行彈框
handleAddDetails(){
let that = this;
that.addCardVisible = true;
that.fileInfo.cardName = '';
that.fileInfo.cardNo = '';
that.fileInfo.startDate = '';
that.fileInfo.endDate = '';
},
// 向表格數(shù)據(jù)中添加一行
addFileDetail(){
let that = this;
if (that.tableData == undefined) {
that.tableData = new Array();
}
let obj = {};
obj.id = 0;
obj.cardName = that.fileInfo.cardName;
obj.cardNo = that.fileInfo.cardNo;
obj.startDate = that.fileInfo.startDate;
obj.endDate = that.fileInfo.endDate;
obj.fileUploadedList=[];
that.tableData.push(obj);
that.addCardVisible = false;
},
// 刪除行
handleDeleteDetails(row){
let that = this;
that.tableData.splice(row.id-1, 1);
//同時(shí) 刪除文件列表及關(guān)聯(lián)列表中的數(shù)據(jù)
let tmpFileName = '';
for(let i=0;i<that.totalFileNameList.length;i++){
if(that.totalFileNameList[i].cardName === row.cardName){
tmpFileName = that.totalFileNameList[i].fileName;// 先暫存再執(zhí)行刪除操作
that.totalFileNameList.splice(i,1);
}
}
for(let i=0;i<that.totalFileList.length;i++){
if(that.totalFileList[i].name === tmpFileName){
that.totalFileList.splice(i,1)
}
}
},
rowClassNameDeal({row, rowIndex}) {
//把每一行的索引放進(jìn)row.id,此方法用于生成表格中的序號,當(dāng)在表格中填寫內(nèi)容時(shí),每一行都需要綁定不同的v-model
row.id = rowIndex+1;
},
// 自定義上傳,只將文件暫存在前端
dynamicUpload(content){
let that = this;
if(content.data.length === 0){
that.$message.warning(`請?zhí)顚懽C照名稱?。。);
that.$refs[content.data].clearFiles();
return false;
}
for(let i=0;i<that.totalFileNameList.length;i++){
if(that.totalFileNameList[i].fileName === content.file.name){
that.$message.warning('改文件已上傳,請重新選擇文件上傳?。?!');
that.$refs[content.data].clearFiles();
return false;
}
}
// 將文件加入到待傳輸?shù)奈募斜?
that.totalFileList.push(content.file)
let fileNameData = {
cardName: content.data,
fileName: content.file.name
}
// totalFileNameList 存儲文件和表格內(nèi)容的關(guān)聯(lián)關(guān)系,這里只關(guān)聯(lián)了證照名稱
that.totalFileNameList.push(fileNameData)
},
// 執(zhí)行上傳操作將文件和表格信息一起發(fā)送到后臺
doFileUpload(){
let that = this;
if(that.totalFileList.length === 0){
that.$message.warning("請上傳文件?。?!");
return;
}
// 上傳文件需要用FormData,processData和contentType 兩個(gè)參數(shù)必須設(shè)置,否則上傳不成功
const params = new FormData();
// 為上傳的每個(gè)文件命名,方便后臺獲取時(shí)與表格數(shù)據(jù)關(guān)聯(lián)
for (let i = 0; i < that.totalFileList.length; i++) {
params.append(that.totalFileList[i].name, that.totalFileList[i]);
}
params.append("fileNameList", JSON.stringify(that.totalFileNameList));
$.ajax({
url:baseurl+"/testupload/fileUpload",
type:"POST",
dataType: "json",
processData: false, //用于對data參數(shù)進(jìn)行序列化處理 這里必須false
contentType: false, //必須
data:params,
success:function(res){
that.$message.warning('上傳成功');
}
});
}
},
created: function(){
}
})
后臺接收代碼
@Controller
@RequestMapping("/testupload")
public class RegisterController {
// 附件從 request中獲取
@RequestMapping("/fileUpload")
@ResponseBody
public ServerResponse fileupload(HttpServletRequest request,String fileNameList){
try{
if(fileNameList == null){
throw new Exception("請選擇文件后上傳!?。?);
}
// 1. 上傳的位置
String path = "E:\\uploadfile";
//判斷該路徑是否存在
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
// 處理以字符串形式上傳的關(guān)聯(lián)數(shù)據(jù)信息
JSONArray jsonArray = JSON.parseArray(fileNameList);
// 遍歷關(guān)聯(lián)列表
for(Object object : jsonArray){
JSONObject jsonObject = JSON.parseObject(object.toString());
System.out.println(jsonObject.getString("cardName"));
// 獲取文件
MultipartFile file1 = ((MultipartHttpServletRequest) request).getFile(jsonObject.getString("fileName"));
// 獲取文件信息
String filename = file1.getOriginalFilename();
System.out.println(filename);
// 保存文件
file1.transferTo(new File(path, filename));
}
}catch (Exception e) {
log.error(e.getMessage());
return ServerResponse.createByErrorMessage(e.getMessage());
}
return ServerResponse.createBySuccess();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue.js中settimeout遇到的問題(時(shí)間參數(shù)短效果不穩(wěn)定)
這篇文章主要介紹了解決vue.js中settimeout遇到的問題(時(shí)間參數(shù)短效果不穩(wěn)定),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
使用el-checkbox-group選中后值為true和false遇到的坑
這篇文章主要介紹了使用el-checkbox-group選中后值為true和false遇到的坑及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vue?cesium加載點(diǎn)與定位到指定位置的實(shí)現(xiàn)方法
Cesium是一個(gè)用于創(chuàng)建高性能、跨平臺的3D地球和地圖的開源JavaScript庫,它提供了許多功能,包括地理空間數(shù)據(jù)可視化、地理定位和地圖導(dǎo)航等,這篇文章主要介紹了vue?cesium加載點(diǎn)與定位到指定位置,需要的朋友可以參考下2024-03-03
Vue3使用dataV報(bào)錯(cuò)問題的解決方法
這篇文章主要為大家詳細(xì)介紹了Vue3中使用dataV報(bào)錯(cuò)問題的解決方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11

