ProgramingTip

HTML5를 사용하여 클라이언트 확인 파일 크기?

bestdevel 2020. 10. 7. 07:57
반응형

HTML5를 사용하여 클라이언트 확인 파일 크기?


HTML5 물결을 타려고하는데 작은 문제가 있습니다. HTML5 이전에는 플래시로 파일 크기를 확인했지만 이제는 웹 앱에서 플래시를 사용하지 않는 것이 추세입니다. HTML5를 사용하여 클라이언트 측에서 파일 크기를 확인하는 방법이 있습니까?


작동합니다. 입력이 변경 될 때 이벤트 리스너 내부에 배치합니다.

if (typeof FileReader !== "undefined") {
    var size = document.getElementById('myfile').files[0].size;
    // check file size
}

허용 된 내용은 선언 파일 수락 입력이 변경 될 때마다 업데이트 이벤트 리스너에 바인딩해야합니다.

document.getElementById('fileInput').onchange = function(){
    var filesize = document.getElementById('fileInput').files[0].size;
    console.log(filesize);    
}

jQuery 라이브러리를 사용하는 경우 다음 코드가 유용 할 수 있습니다.

$('#fileInput').on('change',function(){
  if($(this).get(0).files.length > 0){ // only if a file is selected
    var fileSize = $(this).get(0).files[0].size;
    console.log(fileSize);
  }
});

어떤 것이 당신에게 달려 있는지 표시하기 위해 fileSize의 변환을 감안할 때.


HTML5 파일 크기 확인을 지원합니다.

이 기사를 읽고 파일 API에 대한 자세한 정보를 얻으십시오.

http://www.html5rocks.com/en/tutorials/file/dndfiles/

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


var size = document.getElementById("fileInput").files[0].size;

많은 파일 API는 이름과 유형을 제공합니다.


개인적 으로이 형식을 선택합니다.

    $('#inputFile').bind('change', function(e) {
      var data = e.originalEvent.target.files[0];
      // and then ...
      console.log(data.size + "is my file's size");
      // or something more useful ...
      if(data.size < 500000) {
        // what your < 500kb file will do
      }
    }

"이를 달성하기위한 간단한 브라우저 간 솔루션이 없습니다.": 클라이언트 측에서 파일 업로드 크기를 감지 할 수 있습니까?

참고 URL : https://stackoverflow.com/questions/4112575/client-checking-file-size-using-html5

반응형