0

I am working with files and I don't really understand how to make it work.

I have a web application (c#) that is located on the server and users use this application to upload some information from the files. The problem that I am having that users upload the file (using HttpPostedFileBase class) from their local machines but code is trying to match the same path on the server and of course the file doesn't exist on the server, that's why it throws an error saying that

Cannot find a part of the path ...

If the user is trying to upload the file from C:\Users\User1\Documents\File.txt from their local machine how can I write the code to include the whole path to the file including the computer name and the local drive something like \\ComputerName\c$\Users\User1\Documnets\File.txt.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
MarinaS
  • 13
  • 1
  • 6
  • 1
    Maybe I'm missing something but... why do you need the user's local path to the file at all? You've already stated that the file is being uploaded, so what does the original path of the file matter since it is now stored on the server? – Kritner Dec 22 '14 at 20:27
  • You should refer to either FTP, or HTTP file upload protocol if you are using Web Page on client's machine. Best regards, – Alexander Bell Dec 22 '14 at 20:28
  • Sorry, my fault, I am not uploading the file. I am trying to save the file to the server. If my drive is correctly mapped, I dont have any problems, but to save the file I need to know the place that I am copying it from and the place where I am copying to. In this case because it's a client's machine I dont see the full path to the file to successfully upload it. – MarinaS Dec 22 '14 at 20:43
  • So `\\ComputerName\c$` is actually on the server? Or is it on the client? – Patrick Hofman Dec 22 '14 at 20:44
  • Patrick, I want to access \\ComputerName\c$ from my server, so in this case ComputerName should be the client's machine – MarinaS Dec 22 '14 at 20:46
  • @MarinaS: Then I did get your question correctly. My answer answers that: not possible. – Patrick Hofman Dec 22 '14 at 20:47

1 Answers1

7

You can't. Client and servers are disconnected from each other. That is how the web works. You can't get the full client path and access that in any way from the server.

There are very good reasons for this, the most important one is security / trust.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • How can I save the file from the client's machine to the server than without knowing the full path? – MarinaS Dec 22 '14 at 20:50
  • You have to save it somewhere on the server, depending on your needs. The easiest way it to create a file path yourself, and then save it using [`HttpPostedFileBase.SaveAs`](http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.saveas(v=vs.110).aspx). – Patrick Hofman Dec 22 '14 at 20:51
  • 1
    I am trying the exact the same thing: var fileLocation = Path.Combine(Path.GetTempPath(), string.Join(Guid.NewGuid().ToString(), files.FileName)); files.SaveAs(fileLocation); But it fails to save it saying that the part of the path is not found – MarinaS Dec 22 '14 at 20:54
  • Also note that the first parameter of `string.Join` is the separator. – Patrick Hofman Dec 22 '14 at 20:58
  • fileLocation usually is something like C:\Users\UserName\Documents\FileName – MarinaS Dec 22 '14 at 20:58
  • This is in the error message - C:\Users\UserName\Docuemnts\FileName – MarinaS Dec 22 '14 at 21:01
  • @MarinaS: Is that all? – Patrick Hofman Dec 22 '14 at 21:03
  • I am running my code locally and trying to upload the file from our server: my fileLocation is "C:\\Users\\MarinaS\\AppData\\Local\\Temp\\Install Guide.docx", I can see I can save it in that directory without any problems. But if I do it on the server it throws an error... – MarinaS Dec 22 '14 at 21:08
  • You mean in iis? Does the iis user have a temp directory? Or a user directory at all? – Patrick Hofman Dec 22 '14 at 21:11
  • There is no temp directory, but there is a user directory. – MarinaS Dec 22 '14 at 21:24
  • I guess that is the problem. Try create it or write to another folder. – Patrick Hofman Dec 22 '14 at 21:29
  • I've tested and the Temp folder was hidden, so it's actually there. I really dont know why it's not working how it should. – MarinaS Dec 22 '14 at 21:54
  • Try go change the path to somewhere inside the wen site folder to check if it is an file access issue. – Patrick Hofman Dec 22 '14 at 21:59
  • I've tried in our live environment and it works fine without changing the path. Looks like it was fixed by itself really... – MarinaS Jan 04 '15 at 19:37
  • Okay. Glad you fixed it! – Patrick Hofman Jan 04 '15 at 19:37
  • Argh! Was hoping for a solution. Weird thing is that it "cannot find a part of the path...<*source* path>", not destination. – SteveCav Jan 25 '16 at 04:21