Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Could not save an uploaded file in server while its working fine in local. Thinking that filesaving path is an issue with server.mappath. suggest me where I've gone wrong.. Here is what I did and its working in local. 


What I have tried:

C#
if (file.ContentLength > 0)
          {
              var fileName = Path.GetFileName(file.FileName);
              var location = Path.Combine(Server.MapPath("~/FormUploads"), fileName);
              file.SaveAs(location);
          }
Posted
Updated 6-Feb-19 4:17am
Comments
Richard MacCutchan 6-Feb-19 7:12am    
What does the variable 'location' contain when you try the save?
Member 13794985 6-Feb-19 7:22am    
its getting full path as: D:\NewFolder\SolutionName\ProjectName\FormUploads\background.jpg


tried to give path in different ways, but couldn't find a way to send the saveas path which is not from root directory. help me to figure this out.
Richard MacCutchan 6-Feb-19 8:07am    
Is that a valid path with access permissions for the server? What is the actual error you receive when you run the code?
F-ES Sitecore 6-Feb-19 7:43am    
This is a very well documented task so google for "c# mvc upload and save file" and you'll find lots of examples. If you're getting any error messages then post what they are.

1 solution

I posted a tip about this a number of years ago:

Resolving Paths in a Multi-Folder WebSite[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900