Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i upload a image file uploader, image save in server path and pass image url in image but image not show
XML
<table>
                       <tr>
                           <td width="210px" height="160px">
                               <asp:Image ID="imgMartyr" runat="server" Width="149px" Height="166px" ImageUrl="~/images/noimage.jpg" />
                           </td>
                       </tr>
                       <tr>
                           <td height="20px" style="text-align: center">
                               <asp:FileUpload ID="fuImageMartyr" runat="server" Width="100px" Height="24px" />
                               <asp:Button ID="btnImgMartyr" runat="server" Width="100px" Height="24px" OnClick="btnImgMartyr_Click" Text="Upload" />
                           </td>
                       </tr>
                   </table>


protected void btnImgMartyr_Click(object sender, EventArgs e)
{
if (fuImageMartyr.HasFile)
{
if (fuImageMartyr.PostedFile.ContentLength > 20480)
{
ShowMessage("Image size not grater than 20 KB");
}
else
{
string path = Server.MapPath("../images/") + fuImageMartyr.FileName;
fuImageMartyr.SaveAs(path);
ViewState["Filename"] = fuImageMartyr.FileName;
Stream fs = fuImageMartyr.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
ViewState["Image"] = bytes;
imgMartyr.ImageUrl = path;
ShowMessage("Image uploaded successfully");
}
}
}
Posted
Comments
Arasappan 27-Aug-15 6:06am    
where is the command to insert..
F-ES Sitecore 27-Aug-15 6:13am    
You're setting the url for the image to be the file path of the server. View the source of your page and you'll see the image tag will have that path in its "src" attribute which isn't valid. The ImageUrl needs to be the url path to the image on the server, so something like

"/images/" + fuImageMartyr.FileName

C#
string path = Server.MapPath("../images/") + fuImageMartyr.FileName;
fuImageMartyr.SaveAs(path);
ViewState["Filename"] = fuImageMartyr.FileName;
Stream fs = fuImageMartyr.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
ViewState["Image"] = bytes;
imgMartyr.ImageUrl = path;
try this snippet:
imgMartyr.ImageUrl = "../images/" + fuImageMartyr.FileName;

ShowMessage("Image uploaded successfully");
 
Share this answer
 
Comments
Member 11675009 27-Aug-15 8:12am    
Thanks
C#
string path = Server.MapPath("../images/") + fuImageMartyr.FileName;
fuImageMartyr.SaveAs(path);
ViewState["Filename"] = fuImageMartyr.FileName;
Stream fs = fuImageMartyr.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
ViewState["Image"] = bytes;
imgMartyr.ImageUrl = path;
ShowMessage("Image uploaded successfully");


try this snippet:
imgMartyr.ImageUrl = "../images/" + fuImageMartyr.FileName;
 
Share this answer
 
Comments
Member 11675009 27-Aug-15 8:12am    
Thanks

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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