我有multiple images
个要上传的文件,但我的问题是我想停止上传超过2MB的文件,那么如何使用map而不是for loop检查所有选定文件的大小不超过2MB?>
console.log(this.state.files); // see screenshot
这是控制台:
for (let i = 0; i < this.state.files.length; i++) {
if(this.state.files[i].size > 2000000){
console.log("files is ok");
}else{
console.log("files not allow to more then 2MB");
}
}
答案 0 :(得分:0)
//print the warnings as in OP post
this.state.files.forEach(file =>{
if(file.size < 2000000){
console.log("files is ok");
}else{
console.log("files not allow to more then 2MB");
}
})
//filter array and upload
this.state.files.filter(e=>e.size < 2000000).forEach(e=>{/*upload here*/})
答案 1 :(得分:0)
答案 2 :(得分:0)
如果要检查每个文件是否小于2 MB,请使用every
const areFilesValid = this.state.files.every(f => f.size < 2000000);
if (areFilesValid) {
// continue uploading
} else {
// don't upload
}
(您应该将大小与2 * 1024 * 1024而不是2000000进行比较)
答案 3 :(得分:0)
所有文件:
this.state.files
文件大小小于2000000
let smallSizeFiles = this.state.files.filter((file) => file.size < 2000000 );
文件大小等于或大于2000000
let largeSizeFiles = this.state.files.filter((file) => file.size => 2000000 );