c# - 在 FTP 上传文件工作不正常

c# - uploading file on FTP is not working properly

我正在使用此代码将文件上传到 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。因此,上传文件后,应用程序挂起。这是调试 window

中的输出
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 上有免费的 space。有什么想法吗?

您的控制通道很可能超时了。数据通道很忙,因此很高兴……但是不幸的是,您在代码中设置的任何超时或保持活动都不适用于此处。深入检查您的 FTP 日志应该会显示文件上传然后会话意外关闭。 如果你有防火墙和负载平衡器,也要检查一下(F5 上的默认值对我来说完全一样......)