Html5 preview image before upload JQuery example

Html5 preview image before upload JQuery example, someone asked me to explain?

In this article, I will show you how to preview an image before uploading to the server using jQuery.You can preview the selected photos with the help of HTML5 FileReader(). The FileReader() objects asynchronously read the content files and store on the user’s computer.

In Previous article I explained, Html5 file upload multiple to server using asp.net mvc.

HTML5 upload file example code:

    <style type="text/css">
        .thumb-image {
            float: left;
            width: 100px;
            position: relative;
            padding: 5px;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <div id="wrapper" style="margin-top: 20px; width: 500px; height: 350px; border: 1px solid #d9d3d3;">
        <h2>HTML5 upload image preview</h2>
        <br />
        <input id="fileUpload" multiple="multiple" type="file" />
        <div id="image-holder"></div>
        $(document).ready(function () {
            $("#fileUpload").on('change', function () {
                //Getcount of selected files
                var countFiles = $(this)[0].files.length;
                var imgPath = $(this)[0].value;
                var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
                var image_holder = $("#image-holder");
                if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
                    if (typeof (FileReader) != "undefined") {
                        //loop for each file selected for uploaded.
                        for (var i = 0; i < countFiles;i++) {
                            var reader = new FileReader();
                            reader.onload = function (e) {
                                $("<img />",{
                                    "class": "thumb-image"
                    } else {
                        alert("This browser does not support FileReader.");
                } else {
                    alert("Please select only images");

Description: I have added the div element and input file tag. The user can preview an image in the div tag before uploading it to the server.

Ouput: HTML5 preview image


html5 preview image

Post your comments / questions