Upload Multiple FTP Files

.net, ftp, ftpwebrequest

Solution

You could take a look at sending the files asynchronously if you want to send more than one file at a time (if the order isn't important). Have a look at the various Begin* and End* methods such as FtpWebRequest.BeginGetResponse and FtpWebRequest.EndGetResponse etc.

Additionally you could look into the FtpWebRequest.KeepAlive property, which is used to keep a connection open/cached for reuse.

Hmmm, you could also try creating one giant tar file and send the single file in one stream with one connection ;)

Problem

Requirement, upload 1500 jpg images every night, the below code opens and closes a connection many times, I'm wondering if there is a better way. ...this is a code snippet, so there are variables here that are defined elsewhere ``` Dim picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath), System.Net.FtpWebRequest) Dim picClsStream As System.IO.Stream Dim picCount As Integer = 0 For i = 1 To picPath.Count - 1 picCount = picCount + 1 log("Sending picture (" & picCount & " of " & picPath.Count & "):" & picDir & "/" & picPath(i)) picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath & "/" & picPath(i)), System.Net.FtpWebRequest) picClsRequest.Credentials = New System.Net.NetworkCredential(ftpUsername, ftpPassword) picClsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile picClsRequest.UseBinary = True picClsStream = picClsRequest.GetRequestStream() bFile = System.IO.File.ReadAllBytes(picDir & "/" & picPath(i)) picClsStream.Write(bFile, 0, bFile.Length) picClsStream.Close() Next ``` Some comments: Yes, I know picCount is redundant...It was late at night. ftpImagePath, picDir, ftpUsername, ftpPassword are all variables Yes, this is unencrypted This code works fine, I'm looking to optimize Related Question: FTP Upload multiple files without disconnect using .NET

Original source