파일 업로드에 필요한 유효성검사.
은근히 빼먹기 쉬울 것 같아서 정리한다.
1. 파일 갯수
2. 확장자
3. 파일 사이즈 (모든 파일의 사이즈를 더함)
* 이미 업로드 된 파일의 정보를 가지고 오는 함수에서 attach_count , attach_total_size 를 이미 가져와서 전역변수에 정의해둔 상태이다.
//업로드된 파일 추출
var files = $J('#inputFile_' + param.attach_key)[0].files;
// 파일갯수 유효성검사
if (attach_count+files.length > param.limit_count) {
$J('#inputFile_' + param.attach_key)[0].value = '';
alert(param.limit_count + '개 이하의 파일만 업로드할 수 있습니다.');
return;
}
var files_size = 0
for (var i = 0;i<files.length;i++){
files_size += files[i].size;
var ext = files[i].name.split('.').pop().toLowerCase();
var dot_ext = '.'+ext
// 확장자 유효성검사
console.log(array_allowFileType);
if(param.allow_filetype!="*" && $J.inArray(dot_ext, array_allowFileType) == -1){
alert(ext+'는 등록 할수 없는 파일명입니다.');
return;
}
if($J.inArray(dot_ext, array_denyFileType) != -1 ){
console.log(array_denyFileType);
alert(ext+'는 등록 할수 없는 파일명입니다.');
return;
}
}
// 파일사이즈 유효성검사
if(attach_total_size + files_size > param.limit_size){
alert("업로드 파일 크기는 "+(param.limit_size/1048576)+"MB이하로 제한됩니다.")
$J('#inputFile_' + param.attach_key)[0].value = '';
return;
}
'주말에 쓰는 개발일기 > javascript' 카테고리의 다른 글
chrome 80 samesite 정책 (크롬 / 엣지 결제모듈 인증모듈 사용 후 세션 유실) (1) | 2021.01.08 |
---|---|
ArrayList 함수 clear() UnsupportedOperationException 에러. (0) | 2021.01.08 |
ajax 에러 확인 / err:SyntaxError: Unexpected token < in JSON at position 0 (0) | 2021.01.08 |
feature.fileapi : TypeError: undefined is not a function 에러 (0) | 2021.01.08 |
struts2, ajax, json, IE - FileUploader (0) | 2020.12.25 |