【发布时间】:2015-04-15 20:46:13
【问题描述】:
我正在使用此代码将文件上传到 FTP 服务器:
String source = @"E:\\file\e1.txt";
String ftpurl = @"ftp://ftp.myftpserver.pl";
String ftpusername = @"myusername@myftpserver.pl";
String ftppassword = @"mypassword";
string filename = Path.GetFileName(source);
string ftpfullpath = ftpurl + "/" + filename;
Debug.WriteLine("Uploading...");
var fs = File.OpenRead(source);
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.Timeout = -1;
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
Stream ftpstream = ftp.GetRequestStream();
fs.CopyTo(ftpstream);
ftpstream.Close();
Debug.WriteLine("Upload complete.");
文件很大 - 大约 50 mb。因此,上传文件后,应用程序挂起。这是调试窗口中的输出
Uploading... //immediately after executing the code
The thread 0xd50 has exited with code 0 (0x0). //after some time
而且它从不打印:
Upload complete.
注意,线程退出后,上传仍在进行中,不会取消。我注意到这个问题,当我试图从列表中循环发送多个文件时 - 应用程序在上传第一个文件后挂起。 有人可以帮我吗?
//编辑 出于测试目的,我试图在 for 循环中上传更小的文件。即使在
之后它似乎也有效The thread XXX has exited with code 0 (0x0).
所以这是因为大小,但我的服务器接受更大的文件,我在 ftp 上有可用空间。有什么想法吗?
【问题讨论】:
-
放入try catch并读取异常
-
也不例外。
-
尝试上传越来越小的文件?
-
这次不是 ;) 之前有异常,因为我没有设置 ftp.Timeout = -1,现在异常消失了,但是上传文件后应用程序仍然挂起。
-
小文件没有问题,即使我循环上传它们。我认为大小并不重要,我可以在其他 ftp 客户端上传更大的文件