IT설명서

[자바스크립트] 업로드 이미지 사이즈(넓이, 높이) 구하기

스토리 K 2024. 6. 11. 15:28

자바스크립트를 이용해서 업로드 이미지 사이즈(넓이, 높이) 구하기

<script type="text/javascript">
     window.onload=function(){
        const file = document.getElementById('file');
        file.onchange=function(){
             if(!file.files[0]){
               alert('파일을 선택하세요!');
               return;
            }
            const reader = new FileReader(); 
            reader.readAsDataURL(file.files[0]);
            reader.onload = function(){ 
             const image = new Image();
             image.src = reader.result;
             image.onload=function(){
                 //이미지 파일의 넓이와 높이
                 alert(this.width +", " + this.height);
             };
            };                   
      };
};
</script>

<input type="file" id="file">