我有一个简单的脚本下载/上传文件到FTP。它在Unity 2018.1.9f1上运行良好,但最近我将Unity更新到2018.4.5f1,DownloadFileAsyn崩溃了。它会写入一个空文件,并显示错误"Server returned an error: 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);
}发布于 2019-08-28 14:05:11
我找到了答案。
client.QueryString.Add("filename", FilePath);不知何故,问题就出在这条线上。我用它来识别我在OnFileDownloadCompleted中下载的文件。
对于这一点,userToken似乎是一个更好的选择。
https://stackoverflow.com/questions/57654795
复制相似问题