Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

I am using FileUpload control to upload an image. I am displaying preview of an image when user select an image. I have used following javascript function to display it.

JavaScript
function previewImage(input, elem) {
            //alert(input.files.length)
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#' + elem).attr('src', e.target.result);
                }
                reader.readAsDataURL(input.files[0]);
            }
        }

The above script works fine before postback occurs on the page and preview is available. but when post back occurs FileUpload loses its value. To avoid it I have handle it by using solution given in following post
How to Maintain FileUpload Control’s State after PostBack[^]

The above solution works and fileupload retains its value after postback. But Preview is not working after postback. I am calling the javascript function using following code
C#
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "PreView", "previewImage(" + fu.ID + "," + imgid + ");", true);


the function gets called but it is not able to get files. When I tried to get file length it shows 0. What went wrong? How can I resolved this ?
Posted

You can use the file api if the client supports html5

http://www.html5rocks.com/en/tutorials/file/dndfiles/[^]

Other than that (and maybe Flash etc) there is no way of reading the image from the disc to show a preview.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900