psycoder
업로드 이미지 미리 보기 본문
설명 : 사용자가 지정한 이미지가 업로드 이전에 해당 위치에 놓여져서 미리보기 효과를 얻을 수 있다 또한 확장자를 기준으로 부적합한 파일 업로드를 어느정도 예방한다
function preview_new(img,the_img) {
if (img.length == 0) return;
idx = img.lastIndexOf("\\");
idx2 = filename.lastIndexOf(".");
ext = filename.substring(idx2+1);
ext2 = ext.toLowerCase();
what_img = document.getElementById(the_img);
if ( ext2 == "bmp" || ext2 == "jpg" || ext2 == "gif" || ext2 == "jpeg" || ext2 == "png" ) {
what_img.src = "file://" + img;
} else {
alert('이미지 파일 형식이 아닙니다.\n확인해 주시기 바랍니다.');
what_img.src = "http://www.mydomain.com/images/noimg.gif";
}
}
<img src="images/noimg.gif" name="up_img1" id="up_img1">
<input name="img_file1" type="file" size="50" onchange="preview_new(this.value,'up_img1');">
<img src="images/noimg.gif" name="up_img2" id="up_img2">
<input name="img_file2" type="file" size="50" onchange="preview_new(this.value,'up_img2');">
그즈음 : 이 스크립트는 임원면접에서 떨어진 모 대기업의 입사지원 등록화면에 있던 것을 맘에 들어 발췌한 것이다 반기업 정서가 강하고 모두가 대기업을 욕해도 또한 대기업으로 몰리는 건 아이러니한 일일 것이다 난 그래도 대기업으로부터 이런 스크립트 하나 얻었으니 별 불만 없다고 해야하나
흠~ 일등이 전부는 아닌데
[출처] [javascript]업로드 이미지 미리 보기|작성자 계상준