console
let fileDom = document.getElementById("file");
fileDom.onchange = function(ev){
let e = ev || window.ev;
let isFileValidate = fileValidate(e.target.files[0],['png','jpg','bmp'],5);
console.table(isFileValidate);
if(isFileValidate.type && isFileValidate.len && isFileValidate.size){
uploadFile(e.target.files[0])
}
}
function uploadFile(obj){
console.log("开始上传")
}
function fileValidate(file, allowTypes, maxSize) {
console.log("选中的文件",file)
let theType = file.type.substring(file.type.lastIndexOf('/') + 1);
let allowType = false;
if (allowTypes.includes(theType)) {
allowType = true;
} else {
allowType = false;
}
const allowSize = file.size / 1024 / 1024 < maxSize;
let allowLen = true
async function getLen(){
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(event){
let e = event || window.event;
let image = new Image();
image.src = e.target.result;
image.onload=function(e){
if(image.width < 100 || image.height < 100){
return reject(false)
}else{
return resolve(true);
}
}
}
})
};
getLen().then(val=>{
console.log("1.长宽验证结果",allowLen,val)
return val
}).catch(reason =>{
console.log('2.长宽验证结果',reason);
return reason
})
return {
type: allowType,
size: allowSize,
len: allowLen,
}
}
<h4>打开Console控制台调试工具</h4>
<input class="input-file" type="file" id="file" name="file">