0

I'm trying to get a file upload to work in an application I'm developing.

On my local machine I've been using the following to save the file to a folder on a shared resource:

string path = Path.Combine("\\appname\myfolder$", Path.GetFileName(file.FileName));
file.SaveAs(path);

and everything works fine.

(On the 'appname' server, the folder 'myfolder' is in the root (e.g.) c:\myfolder)

When I deploy my project to IIS on the same server (e.g. http:\appname) this no longer works.

As a result I tried to change the code to point to the c drive on the server it would be running from e.g.

string path = Path.Combine("c:\\myfolder", Path.GetFileName(file.FileName));
                    file.SaveAs(path);

and this didn't work either. Tried a few other things but now lost it completely. Can anybody help?

Mat Richardson
  • 3,576
  • 4
  • 31
  • 56
  • you need to use Server.MapPath to get the physical path to where you want to save, and the app pool identity has to have write permissions to that path. – Brian Driscoll Feb 23 '16 at 14:28
  • You might want to check if the file exists first before using `file.FileName`. – Josh Feb 23 '16 at 14:30
  • What about the user running IIS? Does it have the right permissions? – NicoRiff Feb 23 '16 at 14:32
  • Also, you can get the file directory directly from the file via `Path.GetDirectoryname` http://stackoverflow.com/questions/674479/how-do-i-get-the-directory-from-a-files-full-path – Josh Feb 23 '16 at 14:33

1 Answers1

1

When running this application in IIS, assign an authorized user to the application context, that is hosting the website. By default the worker is running in system context and the operating system (machine account) would require access to the share - which is really not a recommendet solution.

See here how to configure the application pool identity.

Daniel Nachtrub
  • 366
  • 1
  • 6