반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

스터디 용 블로그

이미지 업로드 시 미리보기 javascript 본문

카테고리 없음

이미지 업로드 시 미리보기 javascript

워후 2015. 3. 29. 19:39
반응형

데모

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+")"});

            }

        }

    });    


반응형
Comments