3

I want to preview image which is uploaded using file upload contorl in asp.net before save.

I tried code using filereader but that is not working in IE9 reader is not supported in ie9.

1

3 Answers 3

4

Try this example

Html

<form id="form1" runat="server">
    <input type='file' id="inputFile" />
    <img id="image_upload_preview" src="http://placehold.it/100x100" alt="your image" />
</form>

Script

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

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

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

$("#inputFile").change(function () {
    readURL(this);
});

This will help you.

Working Demo

Sign up to request clarification or add additional context in comments.

Comments

2

How to preview uploaded image in file upload control before save using asp.net or jquery?

This will preview multiple files as thumbnail images at a time

Html

<input id="ImageMedias" multiple="multiple" name="ImageMedias" type="file"
accept=".jfif,.jpg,.jpeg,.png,.gif" class="custom-file-input"  value="">                                    
<div id="divImageMediaPreview"></div>

Script

$("#ImageMedias").change(function () {
    if (typeof (FileReader) != "undefined") {
        var dvPreview = $("#divImageMediaPreview");
        dvPreview.html("");            
        $($(this)[0].files).each(function () {
            var file = $(this);                
                var reader = new FileReader();
                reader.onload = function (e) {
                    var img = $("<img />");
                    img.attr("style", "width: 150px; height:100px; padding: 10px");
                    img.attr("src", e.target.result);
                    dvPreview.append(img);
                }
                reader.readAsDataURL(file[0]);                
        });
    } else {
        alert("This browser does not support HTML5 FileReader.");
    }
});

Working Demo on Codepen

Working Demo on jsfiddle

I hope this will help.

Comments

0

The example of @Manoj works in IE 11 but no need to call the change function:

function readURL(input) {
    if (input.files && input.files[0]) {
    var reader = new FileReader();

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

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

with this code is enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.