elementui上傳文件不允許重名的解決方案
elementui上傳文件不允許重名
需求:
用戶可以多文件上傳 ,在上傳到服務(wù)器之前需要檢查服務(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ù)問(wèn)題
在使用Element UI 的Upload組件,發(fā)現(xiàn)上傳的文件名后綴是重復(fù)的。(eg. test.pdf.pdf)
在檢查了相關(guān)組件的使用,沒(méi)有任何問(wèn)題,最后發(fā)現(xiàn)是windows電腦自動(dòng)會(huì)隱藏文件擴(kuò)展名,導(dǎo)致用戶以為文件沒(méi)有擴(kuò)展名,繼續(xù)修改成帶后綴的文件名導(dǎo)致。
可以點(diǎn)擊文件--查看--勾選擴(kuò)展名,既可避免此類錯(cuò)誤發(fā)生。
到此這篇關(guān)于elementui上傳文件不允許重名的文章就介紹到這了,更多相關(guān)elementui上傳文件不允許重名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目打包解決靜態(tài)資源無(wú)法加載和路由加載無(wú)效(404)問(wèn)題
這篇文章主要介紹了vue項(xiàng)目打包,解決靜態(tài)資源無(wú)法加載和路由加載無(wú)效(404)問(wèn)題,靜態(tài)資源無(wú)法使用,那就說(shuō)明項(xiàng)目打包后,圖片和其他靜態(tài)資源文件相對(duì)路徑不對(duì),本文給大家介紹的非常詳細(xì),需要的朋友跟隨小編一起看看吧2023-10-10
uniapp組件uni-file-picker中對(duì)上傳的圖片進(jìn)行壓縮至1兆以內(nèi)(推薦)
我在做uniapp項(xiàng)目時(shí),用的uni-file-picker組件,這是我做的一個(gè)項(xiàng)目實(shí)例,主要是將圖片通過(guò)接口傳至后臺(tái)服務(wù)器,本文給大家分享uniapp組件uni-file-picker中對(duì)上傳的圖片進(jìn)行壓縮再上傳,感興趣的朋友跟隨小編一起看看吧2022-11-11
nuxt中刷新頁(yè)面后防止store值丟失問(wèn)題
這篇文章主要介紹了nuxt中刷新頁(yè)面后防止store值丟失問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue awesome swiper異步加載數(shù)據(jù)出現(xiàn)的bug問(wèn)題
這篇文章主要介紹了vue awesome swiper異步加載數(shù)據(jù)出現(xiàn)的bug問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Vue中computed及watch區(qū)別實(shí)例解析
這篇文章主要介紹了Vue中computed及watch區(qū)別實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

