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

时间:2016-08-19 18:03:16

标签: c# unity3d ftp

我是团结的新手,我需要一个简单的脚本来发送一个XML文件(不需要阅读内容)来自" StreamingAssets"文件夹到我们的FTP服务器根文件夹,能够更改|" FTP用户名" " FTP密码" " FTP主机名" " FTP端口" |。我在团结论坛和统一文档中看到了一些例子,但没有任何帮助。如果你知道简单的方法,请指导我,谢谢。

我发现了这个但是它没有用。

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 ();
   }

}

3 个答案:

答案 0 :(得分:1)

您根本不是从任何地方调用UploadFile()函数。因为它是一个public函数,我假设它是从另一个脚本调用的,但由于Debug.Log (uri);代码没有显示任何内容,因此可能根本没有调用该函数。你必须从某个地方调用它才能运行它。

出于测试目的,请从Start()函数调用它。将以下代码添加到您的脚本中。

void Start()
{
    UploadFile();
}

请注意,您必须将Uploader脚本附加到场景中的GameObject。必须启用GameObject才能调用Start()函数。

修改

即使这样做,您也会收到错误消息:

The format of the URI could not be determined: blah blah blah

您的"ftp.byethost7.com";链接应为"ftp://byethost7.com";

以下是Unity中的完整FTP上传代码。

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 = "xxx";
    public string FilePath;

    public void UploadFile()
    {
        FilePath = Application.dataPath + "/StreamingAssets/data.xml";
        Debug.Log("Path: " + FilePath);


        WebClient client = new System.Net.WebClient();
        Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);

        client.UploadProgressChanged += new UploadProgressChangedEventHandler(OnFileUploadProgressChanged);
        client.UploadFileCompleted += new UploadFileCompletedEventHandler(OnFileUploadCompleted);
        client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
        client.UploadFileAsync(uri, "STOR", FilePath);
    }

    void OnFileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
    {
        Debug.Log("Uploading Progreess: " + e.ProgressPercentage);
    }

    void OnFileUploadCompleted(object sender, UploadFileCompletedEventArgs e)
    {
        Debug.Log("File Uploaded");
    }

    void Start()
    {
        UploadFile();
    }
}

答案 1 :(得分:0)

更改此:

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

与此:

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

方法OnFileUploadCompleted显示URI尚未"/"
Uri构造函数似乎将文件名与路径名连接在一起而没有格式。 (在Unity 2018.2.3f1中为我工作)。还有其他格式化网址的方法,我只是尝试指出错误。

有史以来最差的英语,我知道...:D

答案 2 :(得分:-1)

let params = {
  template: "some template"
};

$routeProvider.when("/root/cardboards/deparments", params)
.when("/suppliers/:_supplier/cardboards/deparments", params);