3
Answers

Display Image from Folder on Server

toanhoi bui

toanhoi bui

13y
3.2k
1
Hello everbody, I uploaded a image to server and want to show image on Image control by ASP. My code
  String filename = FileUpload1.PostedFile.FileName;
  string strFilename = Server.MapPath("Sample") + "//" + filename;
  FileUpload1.PostedFile.SaveAs(strFilename);
  //Show Image
  Image2.ImageUrl=strFilename ;
But it does not run. Can you help me to show a image on server.Thanks

Answers (3)
1
Dinesh Beniwal

Dinesh Beniwal

Admin 16.9k 5.8m 13y
Hi Toanhoi,

If Satish's answer help you then Mark as accepted answer.

Thanks
-1
toanhoi bui

toanhoi bui

NA 70 169.9k 13y
Thanks I displayed image sucessfully
-1
Satish Bhat

Satish Bhat

NA 1.6k 4.5k 13y
PostedFile.SaveAs(strFilename) takes physical path whereas Image2.ImageUrl accepts virtual path.

Try the following code.


String filename = FileUpload1.PostedFile.FileName;
string strFilename = Server.MapPath("Sample") + "\\" + filename;
FileUpload1.PostedFile.SaveAs(strFilename);
//Show Image
Image2.ImageUrl= "~/Sample/" + filename ;
// Here I am asuming that 'Sample' folder is directly under the application root. Otherwise change the path accordingly.