Results 1 to 3 of 3

Thread: How to FTP multiple files from local machine to FTP site?

  1. #1
    solorize is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2012
    Posts
    7
    Reputation
    0

    How to FTP multiple files from local machine to FTP site?

    Hi,

    I currently have a part of my program that uploads an image file MainChart.png
    from a set location (locally) to a set remote location(on the ftp server).

    What I would now like to do is be able to upload more than one file at the same time.

    What I am currently doing is repeating the same block of code, but changing the image file name
    from MainChart.png to another one of the image file names, so have about 8 instances of this
    block of code, Where the file name is just changed.

    Does anyone know how I can do this in just one block of code, rather than the program
    having to keep calling another Sub and logging onto the FTP site each time for each image
    file that I want to upload?

    I've copied three Sub's of my code to give you an idea how I am currently doing it, please see below:

    Code:
    Private Sub FtpMainChart()
            '// FTP_URL, FTP_UserName & FTP_Password, are variables that hold the FTP credentials, declared earlier in the code.
            '// MainChart.png is the image file that is to be transferred to FTP site.
            Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + FTP_URL + "MainChart.png"), System.Net.FtpWebRequest)
            request.Credentials = New System.Net.NetworkCredential(FTP_UserName, FTP_Password) '// put username and password in here.
            request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    
            '//    \charts\MainChart.png is location on local machine of where File to be uploaded resides.
            Dim file() As Byte = System.IO.File.ReadAllBytes(My.Application.Info.DirectoryPath & "\charts\MainChart.png")
    
            Dim strz As System.IO.Stream = request.GetRequestStream()
            strz.Write(file, 0, file.Length)
            strz.Close()
            strz.Dispose()
        End Sub
    
        Private Sub FtpWindSpeedChart()
            Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + FTP_URL + "WindSpeedChart.png"), System.Net.FtpWebRequest)
            request.Credentials = New System.Net.NetworkCredential(FTP_UserName, FTP_Password) '// put username and password in here.
            request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    
            Dim file() As Byte = System.IO.File.ReadAllBytes(My.Application.Info.DirectoryPath & "\charts\WindSpeedChart.png")
    
            Dim strz As System.IO.Stream = request.GetRequestStream()
            strz.Write(file, 0, file.Length)
            strz.Close()
            strz.Dispose()
        End Sub
    
        Private Sub FtpTemperatureChart()
            Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + FTP_URL + "TemperatureChart.png"), System.Net.FtpWebRequest)
            request.Credentials = New System.Net.NetworkCredential(FTP_UserName, FTP_Password) '// put username and password in here.
            request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    
            Dim file() As Byte = System.IO.File.ReadAllBytes(My.Application.Info.DirectoryPath & "\charts\TemperatureChart.png")
    
            Dim strz As System.IO.Stream = request.GetRequestStream()
            strz.Write(file, 0, file.Length)
            strz.Close()
            strz.Dispose()
        End Sub
    Any help would be appreciated.

    Thanks.

    Mark

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,767
    Reputation
    1702
    There's no managed code to upload multiple files in one go. Your only option is to upload one file multiple times. That said, there's nothing to stop you uploading more than one in parallel. Also, your code is overly complex. Why not simply create a WebClient and call UploadFile or UploadFileAsync?

  3. #3
    solorize is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2012
    Posts
    7
    Reputation
    0
    Thanks jmcilhinney,

    I will have a look into using WebClient Uploadfile and see if I can rewrite my code.

    cheers.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •