스터디 용 블로그
이미지 업로드 시 미리보기 javascript 본문
데모
http://demo.phpgang.com/jquery-show-image-thumbnail-before-upload/
jQuery:
Above jQuery work on onChange event of your file upload field and add image in background of give div.
HTML:
CSS used for image div in which we preview thumbnail:
출처
http://www.phpgang.com/how-to-show-image-thumbnail-before-upload-with-jquery_573.html
+ 추가
여러 개에 적용하기
//공통 기능 : 이미지 업로드 시 미리보기 $("input[type=file]").on("change", function() {
var provider = $(this).attr("id"); var receiver = "preview_"+provider.substring(provider.indexOf("_")+1, provider.length );
// console.log("provider ["+ $(this).attr("id") +"]"+", receiver ["+receiver+"]" ); // console.log("provider.files === "+provider.files);
var files = !!this.files ? this.files : []; if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
if (/^image/.test( files[0].type)){ // only image file var reader = new FileReader(); // instance of the FileReader reader.readAsDataURL(files[0]); // read the local file
reader.onloadend = function(){ // set image data as background of div $("#"+receiver).css({"background-image":"url("+this.result+")"}); } } }); |