2010년 9월 17일 금요일

[Javascript] 파일 upload 전 확장자 / 이미지 크기 체크


  <input type="file" name="fileName" onChange="uploadImg_Change( this, 480,320 )" alt="480*320" >



function fileClear(obj){
obj.select();
document.selection.clear();
obj.blur();
}
function getFileExtension( filePath )
{
    var lastIndex = -1;
    lastIndex = filePath.lastIndexOf('.');
    var extension = "";

if ( lastIndex != -1 )
{
extension = filePath.substring( lastIndex+1, filePath.len );
} else {
extension = "";
}
    return extension;
}

//파일을 선택 후 포커스 이동시 호출
function uploadImg_Change( obj, iwidth, iheight )
{
var value = obj.value;
maxImageHeight=iheight;
maxImageWidth=iwidth;
    var src = getFileExtension(value);
    if (src == "") {
//        alert('올바른 파일을 입력하세요');
fileClear(obj);
        return;
    } else if ( !((src.toLowerCase() == "gif") || (src.toLowerCase() == "jpg") || (src.toLowerCase() == "jpeg")) ) {
        alert('gif 와 jpg 파일만 지원합니다.');
fileClear(obj);
        return;
    }

    LoadImg( obj);

}
function LoadImg(obj)
{
var value = obj.value;
    var imgInfo = new Image();
//    imgInfo.onload = img_Load;
    imgInfo.src = value;
var imgWidth = imgInfo.width;
var imgHeight = imgInfo.height;
if(imgWidth != maxImageWidth || imgHeight != maxImageHeight){
alert(maxImageWidth+"*"+maxImageHeight+"파일만 지원합니다");
fileClear(obj);
return false;
}
}

댓글 없음:

댓글 쓰기