[JS] input file로 가져온 파일이 이미지 인지 확인방법

2020. 2. 24. 11:35자바스크립트

반응형
$("[type='file']").on("change",function(){
     var img = new Image();
        img.onload=function(){
            //파일 존재 시 내용
            console.log("이미지 존재");
        }
        img.onerror=function(){
            //파일 없을 시 내용
            console.log("이미지 미존재");
 
        }
 
    var reader = new FileReader();
        reader.onload = function (e) {
            img.src = e.target.result;
        }
        //Imagepath.files[0] is blob type
        reader.readAsDataURL(this.files[0]);
});​
반응형