jQuery
[JQuery] input file 이미지 파일 등록 업로드, 이미지 미리보기, 이미지 파일 삭제
힘없는염소
2018. 8. 23. 11:52
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> </head> <body> <!-- 이미지 파일만 등록되게 accept 확장지 지정 --> <div id="preview"></div><input type="file" name="" class="inp-img" accept=".gif, .jpg, .png"> <span class="btn-delete">삭제</span> <script src="lib/jquery/2.2.3/jquery.min.js"></script> <script type="text/javascript"> // 등록 이미지 등록 미리보기 function readInputFile(input) { if(input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('#preview').html("<img src="+ e.target.result +">"); } reader.readAsDataURL(input.files[0]); } } $(".inp-img").on('change', function(){ readInputFile(this); }); // 등록 이미지 삭제 ( input file reset ) function resetInputFile($input, $preview) { var agent = navigator.userAgent.toLowerCase(); if((navigator.appName == 'Netscape' && navigator.userAgent.search('Trident') != -1) || (agent.indexOf("msie") != -1)) { // ie 일때 $input.replaceWith($input.clone(true)); $preview.empty(); } else { //other $input.val(""); $preview.empty(); } } $(".btn-delete").click(function(event) { var $input = $(".inp-img"); var $preview = $('#preview'); resetInputFile($input, $preview); }); </script> </body> </html> |
반응형