Uploading multiple files in .NET through FTP
I am thinking of using following code, but I want to transfer hundreds of files and it does not look viable to connect and then disconnect on every file transfer.
request = (FtpWebRequest) FtpWebRequest.Create(FtpAddress + file);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(User, Pass);
request.UsePassive = IsPassive;
request.UseBinary = true;
request.KeepAlive = false;
FileStream fs = File.OpenRead("");
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpStream = request.GetRequestStream();
ftpStream.Write(buffer, 0, buffer.Length);
ftpStream.Close();
开发者_高级运维
What options do I have for uploading all of these files using a single connection?
I have not verified this to be true, but in my quick 30 second search, if you set
request.KeepAlive = true;
on every request you create except the last one, apparently only the first FTPWebRequest makes a full login connection.
Then when you create the last FTPWebRequest, set
request.KeepAlive = false;
and it will close the connection when done. You can verify this if you have access to the FTP server's logs.
精彩评论