user676589
user676589

Reputation:

Upload multiple files to FTP server using vb.net

I am able to upload a single file and now how do I upload multiple files to FTP server :

Here is the code I am working with:

Private Sub uploadFile(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) 'Create FTP request

    Try
        Dim request As FtpWebRequest = DirectCast(FtpWebRequest.Create(FTPAddress & "/" & Path.GetFileName(filePath)), FtpWebRequest)

        request.Method = WebRequestMethods.Ftp.UploadFile
        request.Credentials = New NetworkCredential(username, password)
        request.UsePassive = True
        request.UseBinary = True
        request.KeepAlive = False

        'Load the file
        Dim stream As FileStream = File.OpenRead(filePath)
        Dim buffer As Byte() = New Byte(CInt(stream.Length - 1)) {}

        stream.Read(buffer, 0, buffer.Length)
        stream.Close()

        'Upload file
        Dim reqStream As Stream = request.GetRequestStream()
        reqStream.Write(buffer, 0, buffer.Length)
        reqStream.Close()

        MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
    Catch
        MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
    End Try
End Sub


Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    btnUpload.Enabled = False
    Application.DoEvents()
    uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
    btnUpload.Enabled = True
End Sub

This is how I modified but not working:

 If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim f As New IO.DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)
        For Each file As IO.FileInfo In f.GetFiles
            Select Case file.Extension.ToLower
                Case ".jpg", ".bmp", ".gif", ".png", ".ico"
                    CheckedListBox1.Items.Add(file.FullName, CheckState.Checked)
            End Select
        Next
        For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
            btnUpload.Enabled = False
            Application.DoEvents()
            uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
            btnUpload.Enabled = True
        Next
    End If
End Sub

Upvotes: 4

Views: 16115

Answers (2)

Bryan Crosby
Bryan Crosby

Reputation: 6554

Depending on how you are gathering your list of files, simply iterate over a collection.

I'll just assume a string for example's sake:

Dim files As List(Of String) = New List(Of String)

For Each file In files
  uploadFile(txtFTPAddress.Text, file, txtUsername.Text, txtPassword.Text)
Next

Also, consider the using statement for objects that implement IDisposable.

 Private Sub uploadFile(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) 'Create FTP request

        Try
            Dim request As FtpWebRequest = DirectCast(FtpWebRequest.Create(FTPAddress & "/" & Path.GetFileName(filePath)), FtpWebRequest)

            request.Method = WebRequestMethods.Ftp.UploadFile
            request.Credentials = New NetworkCredential(username, password)
            request.UsePassive = True
            request.UseBinary = True
            request.KeepAlive = False

            Dim buffer As Byte() = Nothing
            'Load the file
            Using stream As FileStream = File.OpenRead(filePath)
                buffer = New Byte(CInt(stream.Length - 1)) {}
                stream.Read(buffer, 0, buffer.Length)
            End Using

            'Upload file
            Using reqStream As Stream = request.GetRequestStream()
                reqStream.Write(buffer, 0, buffer.Length)
            End Using

            MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
        Catch
            MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
        End Try
    End Sub

Upvotes: 3

Chains
Chains

Reputation: 13167

For Each _____ in ______ collection
uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
Next

(filling-int he blanks depends on whatever control you're using to store the file names in.)

Upvotes: 4

Related Questions