I had a file upload that was uploading to a folder in the web application root, i.e. I had
string savePath = @"~/documentation/"
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath(savePath) + filename);
and that worked fine, uploading the file to WebApp/documentation/filename.abc
The problem is, I want to change the documentation location so I don’t have to move that folder when pushing from development to production. So I did the following
In Web.Config:
<appSettings>
<add key="DocumentationLocation" value="C:\Documentation\" />
</appSettings>
In the code:
string savePath = ConfigurationSettings.AppSettings["DocumentationLocation"];
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath(savePath) + filename);
I figured this would work identically, saving the file to the folder specified in the web.config.
However, I get an error when I try to upload a document now, that says:
‘C:\TM_Documentation\’ is not a valid virtual path.
any idea what i’m doing wrong so that i can fix it and save the files outside of the web app directory? Thanks.
Remove the
Server.MapPath(), you don’t need the server to map the path for you, because you are giving a full path already.