Search code examples
c#asp.netwebformsfile-uploadserver.mappath

What is wrong with this file path?


Hai guys,

I have two folders called CSVLoad and Forms... I have an aspx page inside forms folder which has a fileupload control. I save my uploaded file to my CSVLoad folder i gave the following path

FileUpload1.SaveAs(Server.MapPath("CSVLoad//" + FileUpload1.FileName));

I am receiving file not found exception...

Could not find a part of the path 
'F:\WebSites\Payroll\Forms\CSVLoad\Employeesdata.csv'

CSVLoad folder is outside Forms folder (ie) both are root level folders of my application

Answer :

FileUpload1.SaveAs(Server.MapPath("~/CSVLoad//" + FileUpload1.FileName));

from one of previous SO questions ASP.NET Server.Mappath problem from inner folders


Solution

  • If the path does not start with a slash, then it returns a path relative to the current directory that the page is in, in this case Forms I assume. If you want it to map a path relative to the root of your application, then you should prefix a slash on the path and use:

    Server.MapPath("~/CSVLoad/" + FileUpload1.FileName);