ASP.NET - image display when click browse button

Asked By msakt on 24-May-12 07:27 AM
i am using <asp:FileUpload ID="fupBill" runat="server" /> for browse image from location when finish browseimage display on image control
Somesh Yadav replied to msakt on 24-May-12 07:38 AM
Bellowed code enable you to browse file from any location on ur machine, save it in ur application folder, ur image control will display respected image.
e.g. pic.jpg

if (FileUpload1.HasFile)
{
string FileNm = Path.GetFileName(FileUpload1.FileName);
//FileNm=pic.jpg
FileUpload1.SaveAs(@"C:\test\site1\" + FileNm);
Image1.ImageUrl = FileNm;
}
or

The simpliest variant (assume that upload control named as MyFileUpload1 and image named as Image1):

MyFileUpload1.PostedFile.SaveAs(Server.MapPath("~/" + MyFileUpload1.PostedFile.FileName));
Image1.ImageUrl = "~/" + MyFileUpload1.PostedFile.FileName;
Jitendra Faye replied to msakt on 24-May-12 07:58 AM

Use this code-

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<div style="width: 400px; height: 400px; overflow: auto;">
  <asp:Image ID="Image1" runat="server" Visible="false" />
</div>

 

protected void btnUpload_Click(object sender, EventArgs e)
{
  FileUpload1.SaveAs(Server.MapPath("test.jpg"));
  //SET IMAGE URL HERE
  Image1.ImageUrl = "~/test.jpg";
  Image1.Visible = true
;
}


TRy and let me know


try this code and let me know.

dipa ahuja replied to msakt on 24-May-12 09:49 AM



  <asp:FileUpload ID="FileUpload2" runat="server"></asp:FileUpload>
  <asp:Button ID="btnUplopad" Text="Upload file" OnClick="btnUplopad_Click" runat="server" />

 
 protected void btnUplopad_Click(object sender, EventArgs e)
  {
  if (FileUpload2.HasFile)
  {
    string path = Server.MapPath("~/images/") + FileUpload2.PostedFile.FileName; ;
    FileUpload2.SaveAs(path);
    Image1.ImageUrl = path;
  }
  else
  {
    UploadStatusLabel.Text = "You did not specify a file to upload.";
  }
  }