Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving an uploaded file with HttpPostedFileBase.SaveAs in a physical path

I'd like to save an uploaded file to a physical path by the method HttpPostedFileBase.SaveAs().

When I choose a physical path, an exception appears indicates that the path must be virtual.

 var fileName = Path.GetFileName(fileurl.FileName);
 var path = "C:/Projets" + fileName;
 fileurl.SaveAs(Server.MapPath(path));

How can I change my code to be able to save the file every where I want?

like image 205
Lamloumi2 Avatar asked May 30 '13 09:05

Lamloumi2


1 Answers

The Server.MapPath works only with physical locations that are part of the website. If you want to save the file outside you could use the following:

var fileName = Path.GetFileName(fileurl.FileName);
fileurl.SaveAs(Path.Combine(@"c:\projects", fileName));

Make sure though that the account under which your application pool is executing is granted write permissions to this folder.

like image 200
Darin Dimitrov Avatar answered Oct 23 '22 11:10

Darin Dimitrov