【问题标题】:Weird behaviour of DownloadFileAsync (WebClient)DownloadFileAsync (WebClient) 的奇怪行为
【发布时间】:2019-08-26 09:00:24
【问题描述】:

我有一个用于将文件下载/上传到 FTP 的简单脚本。它在 Unity 2018.1.9f1 上运行良好,但最近我将 Unity 更新到 2018.4.5f1 并且 DownloadFileAsyn 坏了。它写入一个空文件,错误为“服务器返回错误:550 没有这样的文件或目录”。该文件在那里,权限是正确的(我确定,因为我的 UploadFile 方法仍然可以正常工作)。我使用 FtpWebRequest(相同的凭据、文件路径等)编写了一个新方法,你猜怎么着——它也可以正常工作!但是 WebClient 下载坏了。

我的 DownloadFileAsync 方法:

    public void DownloadFile(string FilePath)
    {
        Debug.Log("Download Path: " + FilePath);

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

        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnFileDownloadProgressChanged);
        client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(OnFileDownloadCompleted);

        client.QueryString.Add("filename", FilePath);
        client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
        client.DownloadFileAsync(uri, Application.persistentDataPath + "/" + FilePath);
    }

我使用 FtpWebRequest 的新下载方法(它有效,但我更喜欢 WebClient):

public void DownloadFileNew(string FilePath)
{
    Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.Credentials = new NetworkCredential(FTPUserName, FTPPassword);

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);

    using (Stream ftpStream = request.GetResponse().GetResponseStream())
    using (Stream fileStream = File.Create(god.persPath + "/" + FilePath))
    {
        ftpStream.CopyTo(fileStream);
    }

    reader.Close();
    response.Close();
}

最后使用 WebClient 的 UploadFile 方法仍然可以正常工作:

public void UploadFile(string FilePath)
{
    FilePath = Application.persistentDataPath + "/" + FilePath;
    Debug.Log("Upload 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);
}

【问题讨论】:

  • 如果您找到了可行的解决方案,请不要将其作为问题的更新发布,而是将其添加为答案。这样,人们 A) 可以看到您已经找到了解决方案,并且 B) 遇到相同问题的人可以找到您的问题和解决方案
  • 顺便说一句,您应该使用Path.Combine 而不是使用+ "/" + 连接系统路径
  • 感谢您的建议!至于答案而不是更新 - 我现在就去做。

标签: c# unity3d


【解决方案1】:

我找到了答案。

client.QueryString.Add("filename", FilePath);

不知何故,问题出在那一行。我用它来识别我在OnFileDownloadCompleted 下载的文件。

userToken 似乎是一个更好的选择。

【讨论】:

  • 是文件名 url 安全文件名可以包含点为 url 不能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多