0

From my asp.net form, I am trying to save a picture to my SQL Database. Problem is the File Upload button just takes the name of the file, not the full path. When I try to save it, it says file not found. Is there something like OpenFileDialog in webforms, as I use that for vb.net and that works excellent.

Thanks

Furqan Sehgal
  • 4,917
  • 33
  • 108
  • 167

1 Answers1

0

You have to use Server.MapPath method which according to MSDN

specifies the relative or virtual path to map to a physical directory.

Take an example to see Server.MapPath in action for uploading files using asp.net:

'VB.net
If fileUploadControl.HasFile Then
      Dim strFileName As String = System.IO.Path.GetFileName(fileUploadControl.FileName)
      fileUploadControl.SaveAs(Server.MapPath("~/images/") & strFileName)
End If

//C#
if(FileUploadControl.HasFile)
{      
      string filename = Path.GetFileName(FileUploadControl.FileName);

      //Save file to /images directory of web application
      FileUploadControl.SaveAs(Server.MapPath("~/images/") + filename);                      
}
Waqas
  • 6,812
  • 2
  • 33
  • 50
  • I wrote it to vb.net syntax, dim filename = Path.GetFileName(FileUploadControl.FileName) It says path is not declared. – Furqan Sehgal Sep 10 '11 at 11:05
  • Thanks waqas but the code you have given does not look file from chosen path. Error is Could not find a part of the path 'D:\.Net Programs\Asp.Net\ASP.Net DB - Adv\ASP.Net DataBase Basics\images\Lalusar Lake.jpg'. I am looking from d:\MyPics but your code goes somewhere else. – Furqan Sehgal Sep 10 '11 at 17:11
  • you first need to understand how paths in web application works, if you don't want to :) just skip and read the answer here i am sure it will solve your problem http://stackoverflow.com/questions/3422270/how-to-use-server-mappath-to-get-location-outside-website-folder-in-asp-net – Waqas Sep 11 '11 at 11:15