WebRequest from Unity project
本文关键字:project Unity from WebRequest | 更新日期: 2023-09-27 18:21:06
我有一个Unity项目,在其中我将数据存储到本地文件夹中。我想使用ftp客户端将这些数据发送到服务器。我怎么能在Unity做到这一点?这是一种方法吗?有没有像filezilla这样的库或软件可以让我执行?
我遇到了webRequest类,但我不确定如何使用它。我从答案中找到了建议的代码,但我也不确定该如何使用。我从位于onGUI()
函数内的FTPupload
函数中得到了一个错误。
你走在了正确的轨道上。我想这就是你想要的用法:https://github.com/mminer/ftpuploader-unity/blob/master/Editor/Uploader.cs
向下滚动到Upload
方法;我用了很多次,效果很好。
[编辑]以下是可以直接在Unity项目中使用的上述代码示例:
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
}
//////////////////////////////////////////////////////////////////////////