ftp upload of more than one file

Posted on 2007-08-07
Last Modified: 2008-06-23
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 = ""
        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
Question by:kuclu
    LVL 16

    Expert Comment

    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.

    Author Comment

    ok can you pls show me an example of sequential uploading. tnx..
    LVL 16

    Expert Comment

    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.
    LVL 14

    Accepted Solution

    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.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now