locked
.net Core upload File by WebClient FTP RRS feed

  • Question

  • User-1841856025 posted

    Hi all,

    So, I'm trying to upload a file from my client pc to my webserver using a .net core web API. This works fine when I run the web service locally (meaning that the source file and the service are running on the same machine). But when I run the .net core api on my webserver, it cannot find my client computer's local file despite it being fully spelled out. I understand that the API is looking for the file on the webserver at that point, but I'm trying to figure out how to get the webserver to look on my client computer to upload the file when I give it a path. 

    Here is my code as it stands. Again, there is no problem writing to the server, only in finding the client computer's file.

      string fileLocation = @"C:\Temp\Test.txt";
                string fileDestination = "ftp://aaa.com//Test//Test.txt";
    
                try
                {
                    // AsynchronousFtpUpLoader.Upload(fileDestination, fileLocation, ftpUserName,  ftpPassword);
    
                   
                    string To = "ftp://MyIPGoesHere/directory/Test.txt";
    using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential(ftpUserName, ftpPassword); client.UploadFile(fileDestination, WebRequestMethods.Ftp.UploadFile, fileLocation); } } catch (Exception ex) { return BadRequest(new { message = ex.Message + " : " + ex.InnerException }); }

    The error I get is "

    An exception occurred during a WebClient request. : System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\\Temp\\Test.txt'.\r\n 

    
    

    I've seen a lot of posts about this, but never seem to find a clear solution. So I'm sorry if this has been answered elsewhere simply. 

    Thanks in advance!
     

    Monday, May 4, 2020 3:34 PM

Answers

All replies

  • User475983607 posted

    Joshua_W_Wise78

    I understand that the API is looking for the file on the webserver at that point, but I'm trying to figure out how to get the webserver to look on my client computer to upload the file when I give it a path. 

    It's not possible.   Imagine the harm if web server were allowed to connect to client machines and upload files.

    Rethink the design.  Craft a file upload form that allows the user to select the file to upload.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1

    Monday, May 4, 2020 3:44 PM
  • User-1841856025 posted

    Thank you for the link, but the need for redesign isn't the issue. I am going to have only authorized users be able to select files for upload, and I am going to be putting security on this on both ends, I am merely asking, once I have those things, how one can actually refer to the file on the client computer. The hardcoding in the example is just shorthand to test this part of the API independently of the Angular application that it will go behind. 

    That is the point of a webapi, is it not? To be decoupled from the html page? 

    Monday, May 4, 2020 6:05 PM
  • User753101303 posted

    This is not how the web works. A browser is basically a sandbox with very limited access to local resources (and most often with some form of user consent).

    To allow the web server to access client side drives you would need to configure a network share or have a ftp server on each and very workstation.

    Instead if you are just using https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/file the user is able to select a file and send it to the web server without having anything to do on the client side.

    Monday, May 4, 2020 6:14 PM
  • User-1841856025 posted

    Thank you both. 

    I suppose my initial question wasn't phrased correctly, but it seems that what I've gleaned is this: 

    Me: How do I reference a file on the client-side computer with a string?

    Answer: You don't use a string, you need to pass another file type to the web API, since using a string would meant that the API has access to the client computer and that's bad. 

    Okay. 

    So, front-end agnostic (going to use Angular 9, but will also likely create a desktop app and an android app to access this API), what file type do I need to pass to a .net core API endpoint to have it accept a file and, after doing all kinds of security checking (extension, size, user permissions, putting it in the right place) it will save it to a location on the server? 

     

    Monday, May 4, 2020 6:34 PM
  • User475983607 posted

    So, front-end agnostic (going to use Angular 9, but will also likely create a desktop app and an android app to access this API), what file type do I need to pass to a .net core API endpoint to have it accept a file and, after doing all kinds of security checking (extension, size, user permissions, putting it in the right place) it will save it to a location on the server? 

    I'm not sure what you're asking.  Ultimately the file is sent from the client to the server.  You write code in the API Action that saves the file.  The details are up to you. 

    Monday, May 4, 2020 6:56 PM
  • User753101303 posted

    When using a browser, you are using an input type=file control so that the user can select a file. It is then  posted as part of the payload possibly in addition to other form fields. 

    On the server side it is exposed using https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.iformfile?view=aspnetcore-3.1

    See for example https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1 and a Google search should give tons on options.

    With a browser you can"t do much more. With a local application having access to the local hard drive you could use WebClient inside your local app and post to the same location, (it will create the same http request a browser would send).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2020 7:16 PM
  • User-1841856025 posted

    Yes, this is what I'm looking for. Thanks very much.

    Monday, May 4, 2020 7:43 PM