ftp upload of more than one file

Hi. Is it possible to programmatically upload more than one file via ftp to a server?? I managed to upload one file, but can't manage to upload more than one file simultaneously. Below is what i did so far, using VB.NET. thanks.


Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        Const localFile As String = "C:\folder_1\file_1.html"
        Const remoteFile As String = "/public_html/folder/file.html"
        Const host As String = "ftp://ftp.mysite.com"
        Const username As String = "user"
        Const password As String = "*******"
        Dim URI As String = host & remoteFile

        Dim ftp As System.Net.FtpWebRequest = _
            CType(FtpWebRequest.Create(URI), FtpWebRequest)

        ftp.Credentials = New _
            System.Net.NetworkCredential(username, password)

        ftp.KeepAlive = False
        'we want a binary transfer, not textual data
        ftp.UseBinary = True
        'Define the action required (in this case, download a file)
        ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        ftp.UseBinary = True
        ftp.ContentLength = localFile.Length

        Const BufferSize As Integer = 2048
        Dim content(BufferSize - 1) As Byte, dataRead As Integer
        Dim fi As New FileInfo(localFile)

        Using fs As FileStream = fi.OpenRead()
                'open request to send
                Using rs As Stream = ftp.GetRequestStream
                        dataRead = fs.Read(content, 0, BufferSize)
                        rs.Write(content, 0, dataRead)
                    Loop Until dataRead < BufferSize
                End Using
            Catch ex As Exception

                'ensure file closed
            End Try

        End Using

        ftp = Nothing

    End Sub
End Class
Who is Participating?
NBSO_ISSConnect With a Mentor Commented:
Have you thought of using a streamwriter to output FTP commands to a .cmd file and run it using...


That way you can use mput to FTP multiple files.
To upload or download multiple files simultaneously, ftp programs open up multiple connections to the ftp server. Otherwise, you have to upload them sequentially, sending the 2nd file after the first is finished.
kucluAuthor Commented:
ok can you pls show me an example of sequential uploading. tnx..
Just loop through the part of the code that uploads the file in your existing code for each file and don't close the ftp connection until afterwards.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.