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

solorize

Member
Joined
Feb 22, 2012
Messages
7
Programming Experience
Beginner
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:

VB.NET:
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
 
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?
 
Back
Top