鍍金池/ 問(wèn)答/HTML/ input file multiple時(shí),如果檢查文件名不能重復(fù)?

input file multiple時(shí),如果檢查文件名不能重復(fù)?

input file multiple多文件上傳,如何檢查文件名不能重復(fù)?

回答
編輯回答
青黛色

event.files 是你上傳的文件列表

//常規(guī)去重
let flag = false;
for(let i = 0;i<event.files.length-1;i++){
    for(let j = i+ 1;j<event.files.length;j++){
        if(event.files[i].name==event.files[j].name) flag=true
    }
}
//文件名數(shù)組
const arr = Array.from(event.files).map(item=>item.name)
//用set去重
const set = new Set(arr)
//如果set長(zhǎng)度和原來(lái)不同就是有重復(fù)
if(set.length!=arr.length){
    ...
}
2018年6月27日 14:52