elementui上傳文件不允許重名的解決方案
elementui上傳文件不允許重名
需求:
用戶可以多文件上傳 ,在上傳到服務(wù)器之前需要檢查服務(wù)器中有無重名的文件,如果有會(huì)返回重名文件的名稱數(shù)組,這些文件需要一個(gè)一個(gè)的向用戶確認(rèn)是否要覆蓋重傳。確認(rèn)完畢后再上傳到服務(wù)器。
檢查文件重名:
//上傳文件 uploadFile() { let _this = this; // 未選擇文件 if (_this.fileLength === 0) { _this.$message({ message: '請(qǐng)先選擇 [文件] 后在點(diǎn)擊上傳!', type: 'warning' }); return; } // 檢查重名文件 let fileListForm = new FormData(); let noUploadFileList = []; //不覆蓋上傳的 const arrayList = _this.fileList.map(file => file.name); console.log("將要上傳的文件名:", arrayList); arrayList.forEach(fileName => { fileListForm.append("file_name", fileName); }); let fileListConfig = { method: 'post', url: _this.checkFiles, headers: { "Content-Type": "multipart/form-data;charset=utf-8", }, data: fileListForm }; _this.$ajax(fileListConfig) .then(async res => { console.log("檢查是否重復(fù):", res.data); let repeatArray = res.data; // 后端返回重復(fù)文件名數(shù)組 if (repeatArray.length > 0) { for (const file of repeatArray) { await _this.deleteRepeat(file, noUploadFileList); } } console.log("noUploadFileList:", noUploadFileList); //進(jìn)行上傳 //刪除不覆蓋上傳的文件 _this.fileList = _this.fileList.filter(file => !noUploadFileList.includes(file.name)); console.log("新的上傳列表:", _this.fileList); _this.fileLength = _this.fileList.length; if (_this.fileLength === 0) { return; } //進(jìn)行上傳 await _this.performUpload(); }) .catch(err => { console.log(err); }); },
異步函數(shù),一個(gè)一個(gè)文件的確定用戶哪些需要覆蓋上傳,
//file:重名文件 //noUploadFileList:不需要覆蓋上傳的文件名數(shù)組 async deleteRepeat(file, noUploadFileList) { let _this = this; try { // 等待用戶的確認(rèn) await _this.$confirm(file + '文件已上傳至服務(wù)器, 是否覆蓋上傳?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }); // 如果await下面的代碼執(zhí)行了,意味著用戶確認(rèn)覆蓋 _this.$message({ type: 'success', message: '覆蓋文件成功!' }); } catch (error) { // 如果進(jìn)入catch塊,意味著用戶點(diǎn)擊了取消 _this.$message({ type: 'success', message: '已取消文件覆蓋!' }); noUploadFileList.push(file); } },
上傳服務(wù)器:
performUpload() { let _this = this; // 配置請(qǐng)求的相關(guān)參數(shù) //loading開啟 _this.is_loading = true //配置請(qǐng)求的相關(guān)參數(shù) let formData = new FormData() let config = { method: 'post', url: this.uploadUrl, headers: { "Content-Type": "multipart/form-data;charset=utf-8", }, data: formData } console.log("正在上傳:", _this.fileList); //單個(gè)文件,可編輯作者和文件密級(jí) if (_this.fileLength === 1) { formData.append("file", _this.fileList[0].raw) formData.append("author", _this.edit_author) //默認(rèn)編寫人為空,密級(jí)為非密 if (_this.secret_level === '') { _this.secret_level = 0 } formData.append("confidentiality", _this.secret_level) } //多文件 if (_this.fileLength > 1) { _this.fileList.forEach(file => { formData.append("file", _this.fileList.raw) formData.append("author", _this.edit_author) formData.append("confidentiality", 0) }) } //請(qǐng)求后端 _this.$ajax(config) .then(res => { // console.log(res) if (res) { _this.is_loading = false _this.is_done = true if (_this.is_done) { console.log("上傳成功?。。。。?); _this.$message({ message: '上傳成功', type: 'success' }); _this.fileList = [] _this.show = true } _this.edit_author = '' _this.secret_level = '' } else { _this.is_loading = false _this.$message.error('后臺(tái)連接錯(cuò)誤'); _this.fileList = [] console.log("res failed") } }) .catch(err => { _this.is_loading = false _this.$message.error('后臺(tái)連接錯(cuò)誤'); console.log(err) }) },
ELEMENT UI --UPLOAD組件,上傳的文件名后綴重復(fù)問題
在使用Element UI 的Upload組件,發(fā)現(xiàn)上傳的文件名后綴是重復(fù)的。(eg. test.pdf.pdf)
在檢查了相關(guān)組件的使用,沒有任何問題,最后發(fā)現(xiàn)是windows電腦自動(dòng)會(huì)隱藏文件擴(kuò)展名,導(dǎo)致用戶以為文件沒有擴(kuò)展名,繼續(xù)修改成帶后綴的文件名導(dǎo)致。
可以點(diǎn)擊文件--查看--勾選擴(kuò)展名,既可避免此類錯(cuò)誤發(fā)生。
到此這篇關(guān)于elementui上傳文件不允許重名的文章就介紹到這了,更多相關(guān)elementui上傳文件不允許重名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3路由跳轉(zhuǎn)params傳參接收不到的解決辦法
這篇文章主要給大家介紹了關(guān)于vue3路由跳轉(zhuǎn)params傳參接收不到的解決辦法,Vue3是目前前端開發(fā)中非常流行的框架之一,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06vue3的ref,computed,reactive和toRefs你都了解嗎
這篇文章主要為大家詳細(xì)介紹了vue3的ref,computed,reactive和toRefs,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03vue實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)匯率計(jì)算功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)匯率計(jì)算功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01詳解vuex之store拆分即多模塊狀態(tài)管理(modules)篇
這篇文章主要介紹了詳解vuex之store拆分即多模塊狀態(tài)管理(modules)篇,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11Vue動(dòng)態(tài)組件component的深度使用說明
這篇文章主要介紹了Vue動(dòng)態(tài)組件component的深度使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue實(shí)現(xiàn)模糊查詢的簡(jiǎn)單示例
在Vue中實(shí)現(xiàn)模糊查詢,你可以使用JavaScript的filter和includes方法,結(jié)合Vue的v-for指令,本文給大家舉了一個(gè)簡(jiǎn)單的示例,并通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01vue3項(xiàng)目如何配置按需自動(dòng)導(dǎo)入API組件unplugin-auto-import
這篇文章主要介紹了vue3項(xiàng)目如何配置按需自動(dòng)導(dǎo)入API組件unplugin-auto-import問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03