자바스크립트에서 확장자 제한
파일 업로드 할때 확장자 제한을 해야 하는 경우가 무궁무진합니다.
정말 많이 쓰게 되는 코드
<script>
function sub(){
if(f1.uploadFile.value==""){
alert("업로드 할 파일을 지정해 주세요");
return;
}
f2=f1.uploadFile.value;
//그림파일인지 확장자 확인
if(uploadfile_check(f2)){
f1.submit();
//업로드 파일이 그림 파일 형식에 맞으면 전송
}else{
return;
}
}
function uploadfile_check(f2) {
var str_dotlocation,str_ext,str_low;
str_value = f2;
str_low = str_value.toLowerCase(str_value);
str_dotlocation = str_low.lastIndexOf(".");
str_ext = str_low.substring(str_dotlocation+1);
switch (str_ext) {
case "gif" :
case "jpg" :
case "png" :
case "bmp" :
case "tif" :
case "jpe" :
return true;
break;
default:
alert("그림 파일 입력 양식에 맞지 않는 파일입니다.")
return false;
}
}
///////////////////////////////////////////////
HTML에서
<input type=file name=uploadFile onClick="sub()" alt="파일첨부">
요렇게 하면 확장자에 대한 처리가 됨