Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have tried a lot of code but unable to view the uploaded file in image button
Posted
Comments
Sanket Saxena 13-Mar-14 13:51pm    
Not understand what do you want to say dear. Please clear the question with your code then only we understand where you are going wrong.
ZurdoDev 13-Mar-14 13:57pm    
Why? What's wrong?
_AK_ 13-Mar-14 15:15pm    
It all depends on how you have saved the uploaded image.
AndrewCharlz 13-Mar-14 23:52pm    
just save ur image in a physical path once data is saved get that path and rebind the image button ur done with it :)

Try to do this in the following way.

1-Store image in application's local folder as per your desired hierarchy.
2-Store image name in the db.

TO VIEW the upload images, do the following.

Get the images name from the db and use like below to show the images.

<asp:Image ID="imgUploaded" Height="100px" Width="100px" runat="server" data-lightbox="img1" ImageUrl='<%#"~/Images/"+Eval("ImageName") %>' />


EXPLAINATION: Image URL consist of half hard coded path and image name from the datasource(query returned data i.e in EVAL()).


Hope it will help you. For any further query, comment or objection, Most welcomed. :)
 
Share this answer
 
Hi...
C#
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function previewFile() {
            var preview = document.querySelector('#<%=Avatar.ClientID %>');
            var file = document.querySelector('#<%=avatarUpload.ClientID %>').files[0];
            var reader = new FileReader();

            reader.onloadend = function () {
                preview.src = reader.result;
            }

            if (file) {
                reader.readAsDataURL(file);
            } else {
                preview.src = "";
            }
        }
    </script>
</head> 

C#
<input ID="avatarUpload" type="file" name="file"  önchange="previewFile()"   runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<asp:Image ID="Avatar" runat="server" Height="225px" ImageUrl="~/Images/NoUser.jpg" Width="225px" />   

C#
protected void Upload(object sender, EventArgs e)
{
    int contentLength = avatarUpload.PostedFile.ContentLength;//You may need it for validation
    string contentType = avatarUpload.PostedFile.ContentType;//You may need it for validation
    string fileName = avatarUpload.PostedFile.FileName;
    avatarUpload.PostedFile.SaveAs(@"c:\test.tmp");//Or code to save in the DataBase.
}   

Thank u.
 
Share this answer
 
v2

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