1

So far I know how to upload a file to a folder within my solution using the code below.

string root = HttpContext.Current.Server.MapPath("~/upload");

How can I save the file to a different location that is not within a solution i.e to a server location that is mapped to my pc.

string root = HttpContext.Current.Server.MapPath("/Z:/UploadFolder"); I have tried this but its not saving to the server so where I am going wrong?
Junta
  • 23
  • 4
  • 1
    MapPath just replaces the relative path ~\ to your root site with the the correct full operating system pathname on your local machine of that folder. It has nothing to do with sharenames or mapped drives available on your server – Steve Jun 15 '18 at 12:00
  • I think you have misunderstood the purpose of the MapPath function – ADyson Jun 15 '18 at 12:52

3 Answers3

1

You should use MapPath when you have a relative path and want to use the path to your project. for another path you don't need MapPath. just use it like this:

string root ="Z:\\UploadFolder";
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

You can map a virtual directory in IIS to the location you want to save to. For example, map a virtual directory of UploadFolder to z:\uploadfolder. Then, this will work:

string root = HttpContext.Current.Server.MapPath("~/upload");

Make sure you set permissions appropriately.

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
0

Your logic seems to be confusing. Use Server.MapPath - Returns the physical file path that corresponds to the specified virtual path.

But you are passing Physical Location to Server.MapPath in the second statement, which fails the whole purpose of Server.MapPath.

string root = HttpContext.Current.Server.MapPath("/Z:/UploadFolder"); **INCORRECT**

Ideally, you would need to create a virtual directory mapping to "/Z:/UploadFolder" and name it as "upload".

string root = HttpContext.Current.Server.MapPath("~/upload");

NB: You would need to pass explicit credentials to access the network share from ASP.NET. The suggested approach is to use Identity impersonation, once it is done retry with the same logic.

<configuration>
  <system.web>
    <identity impersonate="true" />
  </system.web>
</configuration>
nithinmohantk
  • 459
  • 4
  • 11