vue項(xiàng)目中實(shí)現(xiàn)多文件上傳功能實(shí)例代碼
在vue項(xiàng)目中使用elementui 的 upload 上傳組件,完成多個(gè)文件一次上傳
一、效果圖:
1.選擇上傳的文件:

2、上傳完成:

3、刪除文件:

二、主要的代碼實(shí)現(xiàn)
注意處理列表中已上傳文件何待上傳文件,兩者其實(shí)有區(qū)分的標(biāo)志。我們可以在控制臺(tái)中輸出選擇上傳的文件列表,查看每個(gè)文件的代碼信息,觀察可以發(fā)現(xiàn),如果是已上傳的文件,則文件的狀態(tài)status=“success”,如果是待上傳文件,那么文件的狀態(tài)status=“ready”。我們可以通過(guò)文件的狀態(tài)來(lái)處理已上傳至服務(wù)器和待上傳至服務(wù)器的文件。
主要代碼如下:
<template>
<div>
<el-button type="text" @click="dialogVisible = true">上傳附件</el-button>
<el-dialog width="400px" :visible.sync="dialogVisible" append-to-body>
<div style="float:left">
<el-upload
class="upload-demo"
ref="upload"
:limit="10"
accept=".txt, .txts, .pdf, .docx"
:multiple="true"
action=" "
:on-change="handleFileChange"
:on-remove="onRemove"
:before-remove="beforeRemove"
:on-exceed="fileExceed"
:auto-upload="false"
:file-list="fileList"
>
<el-button slot="trigger" size="small" type="primary">選取附件</el-button>
<el-button style="margin-left: 10px;" v-if="fileList.length>0" size="small" type="success" @click="submitFileForm">上傳附件</el-button>
</el-upload>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitFileForm">確 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import store from "../../../store"
export default{
name:"uploadFile",
data() {
return {
dialogVisible:false,
//回顯附件列表
fileList: [],
//上傳附件列表
files:[],
};
},
mounted(){
//顯示已有附件
this.getFiles()
},
methods:{
//by文章id獲取附件列表
getFiles(){
var articleId=0
articleId=store.state.articleMsg.row.id
console.log(articleId)
this.$store.dispatch('fileManage/getFiles',articleId).then(res=>{
if(res.succeeded){
res.data.forEach(file=>{
this.fileList.push({name:file.fileName,id:file.fileId});
})
}
else
{
this.$message({
type:'error',
message:'獲取附件失敗'
})
}
})
},
//上傳文件之前
beforeUpload(file){
this.fileList.forEach(item=>{
if(isEquael(item.fileName,file.name)){
return this.$message.warning("該文件已存在")
}
})
},
// 上傳發(fā)生變化鉤子
handleFileChange(file, fileList){
this.files = fileList;
this.fileList.push(file)
},
//文件個(gè)數(shù)超過(guò)最大限制時(shí)
fileExceed(file, fileList){
if(this.fileList.length>10){
this.$message.warning("附件個(gè)數(shù)不能超過(guò)10個(gè)")
}
},
//刪除前的鉤子
beforeRemove(file, fileList) {
return this.$confirm(`確定移除 ${ file.name }?`);
},
//刪除的鉤子
onRemove(file,fileList){
if(file.status==="success")
{
var requestData=file.id
this.$store.dispatch('fileManage/deleteFile',requestData).then(res=>{
if(res.succeeded){
this.fileList.pop(file)
this.fileList=[];
this.files = fileList;
this.$message({
type: 'success',
message: '刪除成功!'
});
this.getFiles()
}else{
this.$message({
type: 'error',
message: '刪除失敗!'
});
}
}).catch(()=>{
this.$message({
type: 'error',
message: '請(qǐng)求失敗!'
});
})
}
},
// 提交上傳文件
submitFileForm(){
//判斷是否有文件再上傳
if (this.files.length === 0) {
return this.$message.warning('請(qǐng)選取文件后再上傳')
}
//-- 創(chuàng)建新的數(shù)據(jù)對(duì)象 -->
let formData = new FormData();
//-- 將上傳的文件放到數(shù)據(jù)對(duì)象中 -->
this.files.forEach((file) =>{
formData.append('files',file.raw)
})
//拿到文章id
var articleid=store.state.articleMsg.row.id
formData.append('articleid',articleid)
this.$store.dispatch('fileManage/uploadFile',formData)
.then(res => {
if(res.succeeded){
this.$message.success('上傳成功!');
this.fileList=[]
this.getFiles()
}else{
this.$message.error('上傳失敗');
}
})
.catch(error => {
this.$message.error('上傳失??!');
});
this.dialogVisible=false
}
}
}
</script>總結(jié)
到此這篇關(guān)于vue項(xiàng)目中實(shí)現(xiàn)多文件上傳功能的文章就介紹到這了,更多相關(guān)vue多文件上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue CLI3中使用compass normalize的方法
這篇文章主要介紹了Vue CLI3中使用compass normalize的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
vue+webpack實(shí)現(xiàn)異步組件加載的方法
下面小編就為大家分享一篇vue+webpack實(shí)現(xiàn)異步組件加載的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
解決Element組件的坑:抽屜drawer和彈窗dialog
這篇文章主要介紹了解決Element組件的坑:抽屜drawer和彈窗dialog問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue中設(shè)置el-select的高度不生效問(wèn)題及解決方案
文章介紹了如何使用ElementUI框架中的el-select組件,并解決設(shè)置其高度不生效的問(wèn)題,在Vue2.x中,使用/deep/關(guān)鍵字可以穿透組件作用域修改樣式;在Vue2.6+到Vue3初期,推薦使用::v-deep關(guān)鍵字;在最新的Vue3和構(gòu)建工具中,推薦使用:deep()偽類來(lái)代替::v-deep2025-01-01
Vue——解決報(bào)錯(cuò) Computed property "****" was assigned to but it ha
這篇文章主要介紹了Vue——解決報(bào)錯(cuò) Computed property "****" was assigned to but it has no setter.的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
Element-UI 解決el-table中圖片懸浮被遮擋問(wèn)題小結(jié)
在開發(fā)中,發(fā)現(xiàn)element-ui在el-table中添加圖片懸浮顯示時(shí),會(huì)被單元格遮擋的問(wèn)題,對(duì)于此問(wèn)題解決其實(shí)也并不難,將懸浮圖片放在body節(jié)點(diǎn)下,通過(guò)定位顯示即可,感興趣的朋友跟隨小編一起看看吧2024-06-06

