首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DownloadFileAsync (WebClient)的奇怪行为

DownloadFileAsync (WebClient)的奇怪行为
EN

Stack Overflow用户
提问于 2019-08-26 17:00:24
回答 1查看 168关注 0票数 1

我有一个简单的脚本下载/上传文件到FTP。它在Unity 2018.1.9f1上运行良好,但最近我将Unity更新到2018.4.5f1,DownloadFileAsyn崩溃了。它会写入一个空文件,并显示错误"Server returned an error: 550没有这样的文件或目录“。文件在那里,权限是正确的(我确信这一点,因为我的UploadFile方法仍然工作得很好)。我用FtpWebRequest写了一个新的方法(相同的凭证,文件路径等),猜猜是什么--它也运行得很好!但是WebClient下载坏了。

我的DownloadFileAsync方法:

代码语言:javascript
复制
    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):

代码语言:javascript
复制
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方法,它仍然工作得很好:

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2019-08-28 14:05:11

我找到了答案。

代码语言:javascript
复制
client.QueryString.Add("filename", FilePath);

不知何故,问题就出在这条线上。我用它来识别我在OnFileDownloadCompleted中下载的文件。

对于这一点,userToken似乎是一个更好的选择。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57654795

复制
相关文章

相似问题

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