2015-11-09 81 views
1

我有一個Unity項目,我在其中存儲數據到本地文件夾。我想使用ftp客戶端將這些數據發送到服務器。我如何在Unity中執行此操作?這是否是一種方式?任何圖書館或像我可以執行的filezilla軟件?來自Unity的WebRequest項目

我碰到webRequest class但是我不確定如何使用它。我遇到了答案中的建議代碼,但我不確定我將如何使用它。我在onGUI()函數中的FTPupload函數中發生錯誤。

回答

4

你在正確的軌道上。我想這是你正在尋找的用法: https://github.com/mminer/ftpuploader-unity/blob/master/Editor/Uploader.cs

向下滾動到Upload方法;我用了很多次,效果很好。

[編輯] 這裏的aboves代碼樣本比可以直接在統一項目中使用:

using UnityEngine; 
using System.Collections; 
using System.IO; 
using System; 
using System.Net; 

////////////////////////////////////////////////////////////////////////// 
//CLASS 
////////////////////////////////////////////////////////////////////////// 
public class FTPUpload : MonoBehaviour 
{  
    #region Methods 
    ////////////////////////////////////////////////////////////////////////// 
    //METHODS 
    ////////////////////////////////////////////////////////////////////////// 
    public void CallThisWithButton() 
    { 
     Upload("PATH_TO_YOUR_FILE", "YOUR_FTP_SERVER_ADRESS", "YOUR_USERNAME", "YOUR_PASSWORD", "ANY_SUB_FOLDER"); 
    } 

    /// <summary> 
    /// Uploads a file through FTP. 
    /// </summary> 
    /// <param name="filename">The path to the file to upload.</param> 
    /// <param name="server">The server to use.</param> 
    /// <param name="username">The username to use.</param> 
    /// <param name="password">The password to use.</param> 
    /// <param name="initialPath">The path on the server to upload to.</param> 
    static void Upload(string filename, string server, string username, string password, string initialPath) 
    { 
     Debug.Log("Upload started."); 

     var file = new FileInfo(filename); 
     var address = new Uri("ftp://" + server + "/" + Path.Combine(initialPath, file.Name)); 
     var request = FtpWebRequest.Create(address) as FtpWebRequest; 

     // Upload options: 

     // Provide credentials 
     request.Credentials = new NetworkCredential(username, password); 

     // Set control connection to closed after command execution 
     request.KeepAlive = false; 

     // Specify command to be executed 
     request.Method = WebRequestMethods.Ftp.UploadFile; 

     // Specify data transfer type 
     request.UseBinary = true; 

     // Notify server about size of uploaded file 
     request.ContentLength = file.Length; 

     // Set buffer size to 2KB. 
     var bufferLength = 2048; 
     var buffer = new byte[bufferLength]; 
     var contentLength = 0; 

     // Open file stream to read file 
     var fs = file.OpenRead(); 

     try 
     { 
      // Stream to which file to be uploaded is written. 
      var stream = request.GetRequestStream(); 

      // Read from file stream 2KB at a time. 
      contentLength = fs.Read(buffer, 0, bufferLength); 

      // Loop until stream content ends. 
      while (contentLength != 0) 
      { 
       //Debug.Log("Progress: " + ((fs.Position/fs.Length) * 100f)); 
       // Write content from file stream to FTP upload stream. 
       stream.Write(buffer, 0, contentLength); 
       contentLength = fs.Read(buffer, 0, bufferLength); 
      } 

      // Close file and request streams 
      stream.Close(); 
      fs.Close(); 
     } 
     catch (Exception e) 
     { 
      Debug.LogError("Error uploading file: " + e.Message); 
      return; 
     } 

     Debug.Log("Upload successful."); 
    } 
    ////////////////////////////////////////////////////////////////////////// 
    #endregion 
} 
////////////////////////////////////////////////////////////////////////// 
+0

我已經跨越建議類別來了。不過,我不知道我將如何使用它從我的代碼。我想要一個按鈕,當按下按鈕發送數據到我的服務器使用ftpuploader。我將文件複製到我的項目中,並且出現FTPUpload錯誤(方法或類?)我沒有定義錯誤。 –

+0

不應該上傳而不是FTPUpload? –

+0

@JoseRamon我編輯了我的第一個答案,以便您可以找到一個代碼示例,您應該可以將其複製/粘貼到您的項目中。 請記住我使用UI 4.6按鈕調用'CallThisWIthButton()'方法。另外,請確保您的文件路徑正確:\\而不是\(我的看起來像'C:\\ Users \\ Roy \\ Desktop \\ test_file.txt',並且您的目標路徑是FTP服務器已經存在;) – Kardux