首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Unity 3D简单C#文件发送到FTP服务器示例脚本

Unity 3D简单C#文件发送到FTP服务器示例脚本
EN

Stack Overflow用户
提问于 2016-08-20 02:03:16
回答 2查看 7.1K关注 0票数 2

我刚接触unity,我需要一个简单的脚本来将一个XML文件(不需要读取内容)从"StreamingAssets“文件夹发送到我们的FTP服务器根文件夹,并且能够更改|"FTP User Name”"FTP Password“"FTP Host Name”"FTP Port"|。我在unity论坛和unity文档中看到了一些例子,但都没有帮助到我。如果你知道简单的方法,请指导我,谢谢。

我找到了这个,但它不起作用。

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.IO;

public class Uploader : MonoBehaviour
{
public string FTPHost     = "ftp.byethost7.com";

public string FTPUserName = "b7_18750253";

public string FTPPassword = "**********";

public string FilePath;

public void UploadFile()
{


    WebClient client = new System.Net.WebClient();


    Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);

    Debug.Log (uri);


    client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);


    client.UploadFileAsync(uri, "STOR", FilePath);

}

void start()
   {
    FilePath = Application.dataPath+"/StreamingAssets/data.xml";

    UploadFile ();
   }

}
EN

回答 2

Stack Overflow用户

发布于 2018-08-21 00:22:24

更改此设置:

代码语言:javascript
复制
Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);

有了这个:

代码语言:javascript
复制
Uri uri = new Uri(FTPHost + "/" + new FileInfo(FilePath).Name);

方法OnFileUploadCompleted显示URI没有"/"

Uri构造函数似乎将文件名与路径名连接在一起,但没有格式。(适用于Unity 2018.2.3f1)。还有其他方法可以格式化url,我只是试着指出错误。

我知道这是有史以来最烂的英语。:D

票数 2
EN

Stack Overflow用户

发布于 2018-12-18 04:26:16

代码语言:javascript
复制
using System.IO;

public void UploadFile2()
    {
        FilePath = Application.dataPath + "/StreamingAssets/ARITMETICA3DAZ1.txt";
        Debug.Log("Path: " + FilePath);

    Uri uri = new Uri(FTPHost + "/" + new FileInfo(FilePath).Name);
    FtpState state = new FtpState();
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    request.Credentials = new NetworkCredential (FTPUserName,FTPPassword);

    state.Request = request;
    state.FileName = FilePath;

    request.BeginGetRequestStream(
        //new AsyncCallback (EndGetStreamCallback), 
        new AsyncCallback (EndGetStreamCallback), 
        state
    );




    public class FtpState
    {
        private ManualResetEvent wait;
        private FtpWebRequest request;
        private string fileName;
        private Exception operationException = null;
        string status;

        public FtpState()
        {
            wait = new ManualResetEvent(false);
        }

        public ManualResetEvent OperationComplete
        {
            get {return wait;}
        }

        public FtpWebRequest Request
        {
            get {return request;}
            set {request = value;}
        }

        public string FileName
        {
            get {return fileName;}
            set {fileName = value;}
        }
        public Exception OperationException
        {
            get {return operationException;}
            set {operationException = value;}
        }
        public string StatusDescription
        {
            get {return status;}
            set {status = value;}
        }
    }
    public class AsynchronousFtpUpLoader
    {  
        // Command line arguments are two strings:
        // 1. The url that is the name of the file being uploaded to the server.
        // 2. The name of the file on the local machine.
        //
        public static void Main(string[] args)
        {
            // Create a Uri instance with the specified URI string.
            // If the URI is not correctly formed, the Uri constructor
            // will throw an exception.
            ManualResetEvent waitObject;

            Uri target = new Uri (args[0]);
            string fileName = args[1];
            FtpState state = new FtpState();
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example uses anonymous logon.
            // The request is anonymous by default; the credential does not have to be specified. 
            // The example specifies the credential only to
            // control how actions are logged on the server.

            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            // Store the request in the object that we pass into the
            // asynchronous operations.
            state.Request = request;
            state.FileName = fileName;

            // Get the event to wait on.
            waitObject = state.OperationComplete;

            // Asynchronously get the stream for the file contents.
            request.BeginGetRequestStream(
                new AsyncCallback (EndGetStreamCallback), 
                state
            );

            // Block the current thread until all operations are complete.
            waitObject.WaitOne();

            // The operations either completed or threw an exception.
            if (state.OperationException != null)
            {
                throw state.OperationException;
            }
            else
            {
                Console.WriteLine("The operation completed - {0}", state.StatusDescription);
            }
        }
        private static void EndGetStreamCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState) ar.AsyncState;

            Stream requestStream = null;
            // End the asynchronous call to get the request stream.
            try
            {
                requestStream = state.Request.EndGetRequestStream(ar);
                // Copy the file contents to the request stream.
                const int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];
                int count = 0;
                int readBytes = 0;
                FileStream stream = File.OpenRead(state.FileName);
                do
                {
                    readBytes = stream.Read(buffer, 0, bufferLength);
                    requestStream.Write(buffer, 0, readBytes);
                    count += readBytes;
                }
                while (readBytes != 0);
                Console.WriteLine ("Writing {0} bytes to the stream.", count);
                // IMPORTANT: Close the request stream before sending the request.
                requestStream.Close();
                // Asynchronously get the response to the upload request.
                state.Request.BeginGetResponse(
                    new AsyncCallback (EndGetResponseCallback), 
                    state
                );
            } 
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine("Could not get the request stream.");
                state.OperationException = e;
                state.OperationComplete.Set();
                return;
            }

        }

        // The EndGetResponseCallback method  
        // completes a call to BeginGetResponse.
        private static void EndGetResponseCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState) ar.AsyncState;
            FtpWebResponse response = null;
            try 
            {
                response = (FtpWebResponse) state.Request.EndGetResponse(ar);
                response.Close();
                state.StatusDescription = response.StatusDescription;
                // Signal the main application thread that 
                // the operation is complete.
                state.OperationComplete.Set();
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine ("Error getting response.");
                state.OperationException = e;
                state.OperationComplete.Set();
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39045249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档