Search code examples
c#unity-game-engineftpfilezilla

Communicate from Unity to a FileZilla server


I have create some files from Unity and I want to send them to local server I have created (using FileZilla server). Any idea how can I communicate from the unity project I have to the server I have create with FileZilla? I am trying to implement the suggested code, I got the following error:

UriFormatException: Invalid URI: Invalid port number System.Uri.Parse (UriKind kind, System.String uriString) System.Uri.ParseUri (UriKind kind) System.Uri..ctor (System.String uriString, Boolean dontEscape) System.Uri..ctor (System.String uriString) recorgingX.UploadFile (System.String filepath) recorgingX.OnGUI ()

As m_FtpHost i gave the

ftp:// + ip + portID

EDIT: I change the slash characters and I no longer get that issue. Now my problem is that while I call UploadFile (outName); it doesnt upload it to the server. How can I check what is going on? The proposed code is working fine in c# project however when imported to a UNity project it doesnt do anything. In the filezilla server I am receiving the following:

10/2015 17:18:52 PM - (not logged in) (127.0.0.1)> USER userID
(000025)11/10/2015 17:18:52 PM - (not logged in) (127.0.0.1)> 331 Password required for chrathan
(000025)11/10/2015 17:18:58 PM - (not logged in) (127.0.0.1)> PASS ***********
(000025)11/10/2015 17:18:58 PM - (not logged in) (127.0.0.1)> 530 Login or password incorrect!

Solution

  • You can use the System.Net.WebClient class to upload a file to an FTP server.

    Here's a theoretical example of how to do that.

    using System;
    using System.Net;
    
    public string m_FtpHost = "ftp://myftpserver.com";
    public string m_FtpUsername = "FTP username";
    public string m_FtpPassword = "FTP password";
    
    public void UploadFile(string filepath)
    {
        // Get an instance of WebClient
        WebClient client = new System.Net.WebClient();
        // parse the ftp host and file into a uri path for the upload
        Uri uri = new Uri(m_FtpHost + new FileInfo(filepath).Name);
        // set the username and password for the FTP server
        client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
        // upload the file asynchronously, non-blocking.
        client.UploadFileAsync(uri, "STOR", filepath);
    }
    

    Documentation for WebClient and the other required classes can be found on MSDN https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx