Pages

Tuesday, August 31, 2010

Error : ASP.net The given path's format is not supported

This error occurs when you try to save the file in the desired folder using the ASP.net file upload control.

HttpPostedFile myFile = fileUpload.PostedFile;

myFile.SaveAs(Server.MapPath("\\news\\") + myFile.FileName);

The above line will generate the error. As it will try to save the file in the path :
C:\home\sites\xyz\news\C:\Upload\filetoupload.txt

which is invalid format because of c:\ being used twice and hence you getting the
invalid format error.

When working with file names and paths it's recommended to use System.IO.Path-class 

So use
myFile.SaveAs(Server.MapPath("\\news\\") + System.IO.Path.GetFileName(myFile.FileName));

Hoping that this will help.
:)