Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hej agian
im trying to preview my image before uploading it to my db , so i used javascript its work with html img but not asp image .

so now im trying to save image into my database from html tag img.
the problem i cant catch the element img ! in code behinde
is there any possible way to store in my database_

What I have tried:

function showpreview(input) {

if (input.files && input.files[0]) {

var reader = new FileReader();
reader.onload = function (e) {
$('#imgpreview').css('visibility', 'visible');
$('#imgpreview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}

}
Posted
Updated 21-Sep-18 3:38am

1 solution

You can't get the content of an image tag, img or asp:Image. You can show the preview if you want, but you have to submit the file as part of a form submission and read it as a normal file.

Google "asp.net upload image file" and you'll find examples of how to do this.

<asp:FileUpload ID="myFile" runat="server" onchange="showpreview(this);" />

<img id="imgpreview" />

<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" runat="server" />

<script type="text/javascript">
    function showpreview(input) {

        if (input.files && input.files[0]) {

            var reader = new FileReader();
            reader.onload = function (e) {
                $('#imgpreview').css('visibility', 'visible');
                $('#imgpreview').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }

    }
</script>
 
Share this answer
 
v2
Comments
Member 13990122 21-Sep-18 9:44am    
Hello
Good evening
Yes I have Googled alot I have to save it first to dB then restore from dB
My first problem how to view it under file upload before send it to dB.
I'll Google agian
And thanks for answering
F-ES Sitecore 21-Sep-18 10:04am    
You just call your js function from the input file's change event, I've updated my solution with an example

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