[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

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()
            Try
                'open request to send
                Using rs As Stream = ftp.GetRequestStream
                    Do
                        dataRead = fs.Read(content, 0, BufferSize)
                        rs.Write(content, 0, dataRead)
                    Loop Until dataRead < BufferSize
                    rs.Close()
                End Using
            Catch ex As Exception

            Finally
                'ensure file closed
                fs.Close()
            End Try

        End Using

        ftp = Nothing

    End Sub
End Class
0
kuclu
Asked:
kuclu
  • 2
1 Solution
 
TSmoothCommented:
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.
0
 
kucluAuthor Commented:
ok can you pls show me an example of sequential uploading. tnx..
0
 
TSmoothCommented:
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.
0
 
NBSO_ISSCommented:
Have you thought of using a streamwriter to output FTP commands to a .cmd file and run it using...

System.Diagnostics.Process.Start("yourfile.cmd")

That way you can use mput to FTP multiple files.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now