Sunday, 3 November 2013

Image preview using jquery

Hi today i'll explain how to show image preview for an uploaded image using jquery which will work in IE9 & IE10 hve a look in bellow code which an simple sampe to do the image preview.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){  
        $('#imgInput').live('change',function(){
            readURL(this);
        });
    });
function readURL(input) {
var isIE = (navigator.appName=="Microsoft Internet Explorer");
var path = input.value;
            var $prev = $('#blah');
if(isIE) {
        if($("imgInput").val()!=''){
        $prev.attr('src', path);
 $prev.show();
 }
 else{
 $prev.hide();
        }
       }
      else{
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                    reader.onload = function (e) {
                        $prev.attr('src', e.target.result);
                    }

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

                $prev.show();
            } else {
                $prev.hide();
            }
}
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <input id="imgInput" type='file' />
        <img id="blah" src="#" alt="your image" style="display:none; width:100px;height:100px;border:1px solid red;" />
    </form>
</body>
</html>

No comments:

Post a Comment