Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Display image before upload using javascript/jQuery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 223
    Comment on it

    This post demonstrates how to display an image prior to uploading it on server.
    This demo uses the JavaScript's readAsDataURL method of reader API which will read image as url.

    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        </head>
    
        <body>
            <img id="thumbnail" src="test.png" alt="Image"/>
            <div style="clear: both;"></div>
            <input type="file" name="file" id="file"/>
            <div style="clear: both;"></div>
            <button id="btn">Click</button>
    
    <script type="text/javascript">
        $(document).ready(function() {
            $(document).on('click', '#btn', function() {
                console.log('sss');
                var reader = new FileReader();
                console.log(reader);
                reader.onload = function (e) {
    
                    $('#thumbnail').attr('src', e.target.result);
                    }
    
                console.log(file);
                reader.readAsDataURL(file.files[0]);
                });
            });
    </script>
        </body>
    </html>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: