23 February 2017

How to Display Image in File Upload

How to Display Image in File Upload

This is simple trick how to display image in file upload before upload to server.

Html

    <form id="form1" runat="server">
        <input type='file' id="imgInp" />
        <img id="blah" src="#" alt="your image" />
    </form>

Javascript


    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
         
            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }
         
            reader.readAsDataURL(input.files[0]);
        }
    }
 
    $("#imgInp").change(function(){
        readURL(this);
    });

Try this sample here

No comments: