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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
Server.MapPath is for virtual path. You can try to use Path.GetFullPath(path).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0