모지리도 정리한다.


#이미지 미리보기#


  <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>

    <script>

        $(function() {

            $("#a").on('change', function(){

                readURL(this);

            });

        });


        function readURL(input) {

            if (input.files && input.files[0]) {

            var reader = new FileReader();


            reader.onload = function (e) {

                    $('#b').attr('src', e.target.result);

                }


              reader.readAsDataURL(input.files[0]);

            }

        }

    </script>

</head>

<body>

    <form id="" runat="server">

        <input type='file' id="a" />

        <img id="b" src="#" alt="" />

    </form>

</body>

</html>


구글링하다보면 돌아다니는 소스들이다. 개인적으로 이미지 미리보기 로직 구현할 때 알아보기 쉽고, 사용하기 쉬워 많이 선호한다. 그리고 이미지가 없을 때는 엑박모양이 등장하는데, 가리고 싶다면, img태그에 src를 제거하면 엑박모양을 사라질 것이다. 
( 등록게시판 구현 중, 이미지 업로드를 하지 않았는데 , 엑박모양이 나와, 의아했던 경험이 있다.) 
마지막으로 대-소문자 구분잘하자.