Upload Multiple files to a ftp server

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

59 Replies - 10199 Views - Last Post: 23 January 2013 - 11:04 AM Rate Topic: ***** 1 Votes

#16 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 10:48 PM

I'm saying that you should rearrange (actually write a line of code, or two) your ForEach loop, so it can upload each file with New WebClient. I strongly suggest that you read both linked blog posts at the top of my signature.
Was This Post Helpful? 1
  • +
  • -

#17 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 10:51 PM

Used
WebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))


still get the same error and now nothing is getting uploaded
Was This Post Helpful? 0
  • +
  • -

#18 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 11:36 PM

i declared webclient as a new webclient and Yes i created a new webclient with the above line of coding but i still get the same error. But now not even the file with index (0) is getting uploaded
 Dim webClient As New WebClient

        Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()
        If (dr = System.Windows.Forms.DialogResult.OK) Then
            ' Read the files 
            Dim file As String
            Dim N As Integer = 0
            For Each file In OpenFileDialog1.FileNames

                Try

                    ListBox1.Items.Add(file)

                    Dim myUri As New Uri("ftp://testingbluezap.vacau.com/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N))
                    myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential("", "")

                    'Now you do the uploading
                    WebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))


                Catch SecEx As SecurityException
                    ' The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    ' Could not load the File - probably permissions-related.
                    MessageBox.Show(("Cannot Select File: " & file.Substring(file.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
                    N = N + 1
                End Try

            Next file


        End If


Was This Post Helpful? 0
  • +
  • -

#19 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 11:44 PM

Bluezap you responded in 2 minutes, after my previous post. Why don't you try to do something by yourself, and think why you are getting errors? I'll say again: do yourself a favor, and read both linked blog entries at the top of my signature, because you are acting exactlly like that.

I gave you all the information you need in this topic, and previous one, where you wanted to upload just one file to FTP.

I'm not sure, if you'd be capable of wrapping a single file uploading routine to a custom class with private WebClient, respond to it's events and rise your own custom events, than handle them in the calling class/module with custom event arguments, if you continue to just copy/paste given code lines, and respond in style: "It still doesn't work.".

I actually opened the Visual Studio for you, just to double check that I'm not leading you the wrong way, and I wrote perfectly working solution in under 5 minutes (without responding to each WebClient's events).
Was This Post Helpful? 2
  • +
  • -

#20 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 02 September 2012 - 09:53 AM

View Postlucky3, on 01 September 2012 - 11:44 PM, said:

Why don't you try to do something by yourself, and think why you are getting errors? I'll say again: do yourself a favor, and read both linked blog entries at the top of my signature, because you are acting exactlly like that.

Im sorry man im just frustrated over this whole thing because i cant seem to get it right :/
Thanks for all your help you really did help alot
I'll try something and try to find the solution :)

This post has been edited by Bluezap: 02 September 2012 - 09:54 AM

Was This Post Helpful? 0
  • +
  • -

#21 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 02 September 2012 - 10:01 AM

That's the way to go! :smile2: I'll give you a hint: when you're "bringing to life" new instance of class, you actually must use New keyword.
Was This Post Helpful? 1
  • +
  • -

#22 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 02 September 2012 - 10:58 PM

View Postlucky3, on 02 September 2012 - 10:01 AM, said:

That's the way to go! :smile2: I'll give you a hint: when you're "bringing to life" new instance of class, you actually must use New keyword.
I FINALLY figured it out!!! but now the problem is that the program gets stuck until the files are uploaded and no progress is shown?

        Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()
        If (dr = System.Windows.Forms.DialogResult.OK) Then
            ' Read the files 
            Dim file As String
            Dim N As Integer = 0
            For Each file In OpenFileDialog1.FileNames

                Try

                    ListBox1.Items.Add(file)
                    Dim myFtpUploadWebClient As New WebClient
                    Dim myUri As New Uri("ftp://testing.com/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N))
                    myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential("usrname", "pass")

                    'Now you do the uploading
                    myFtpUploadWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))


                Catch SecEx As SecurityException
                    ' The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    ' Could not load the File - probably permissions-related.
                    MessageBox.Show(("Cannot Select File: " & file.Substring(file.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))

                End Try
                N = N + 1
            Next file


        End If


should i use a backgroundworker to make it respond properly and get the progress while uploading the files? or is there another way?
Was This Post Helpful? 0
  • +
  • -

#23 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 06:10 AM

View PostBluezap, on 02 September 2012 - 10:58 PM, said:

I FINALLY figured it out!!! but now the problem is that the program gets stuck until the files are uploaded and no progress is shown?
...
should i use a backgroundworker to make it respond properly and get the progress while uploading the files? or is there another way?


Now you need to provide capability for intercepting, and responding to events, raised from objects (each instance of myFtpUploadWebClient in your case). That means you need to "register" to those events. This is done with AddHandler (read the description on MSDN).

You might want to do this right after instantiating new WebClient object. For a start, register just for UploadFileCompleted event, and delegate it to myFtpUploadWebClient_UploadFileCompleted method. In myFtpUploadWebClient_UploadFileCompleted submethod, (for now) just show messagebox with info text like "File uploaded!"
Was This Post Helpful? 1
  • +
  • -

#24 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 09:58 AM

View Postlucky3, on 03 September 2012 - 06:10 AM, said:

Now you need to provide capability for intercepting, and responding to events, raised from objects (each instance of myFtpUploadWebClient in your case). That means you need to "register" to those events. This is done with AddHandler (read the description on MSDN).

You might want to do this right after instantiating new WebClient object. For a start, register just for UploadFileCompleted event, and delegate it to myFtpUploadWebClient_UploadFileCompleted method. In myFtpUploadWebClient_UploadFileCompleted submethod, (for now) just show messagebox with info text like "File uploaded!"

Like this?
Private WithEvents myFtpUploadWebClient As New WebClient


    'here you track what happened when upload completes
    Private Sub myFtpUploadWebClient_UploadFileCompleted(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs) Handles myFtpUploadWebClient.UploadFileCompleted

        'If we didn't succeed, we want to know what went wrong
        If e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)

        Else
            MessageBox.Show("File upload complete!")
        End If
    End Sub

    'and here we listen to WebClient's event when UL progress changes
    Private Sub myFtpUploadWebClient_UploadProgressChanged(ByVal sender As Object, ByVal e As System.Net.UploadProgressChangedEventArgs) Handles myFtpUploadWebClient.UploadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub


This post has been edited by Bluezap: 03 September 2012 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

#25 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 10:08 AM

View PostBluezap, on 03 September 2012 - 09:58 AM, said:

Like this?
'You don't need this, because you were having Dim myFtpUploadWebClient As New WebClient in 
'For Each file... loop in your previous post
'Private WithEvents myFtpUploadWebClient As New WebClient


    'here you track what happened when upload completes
    Private Sub myFtpUploadWebClient_UploadFileCompleted(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs) 'Handles myFtpUploadWebClient.UploadFileCompleted - you can't use this, because you will AddHandler to each WebClient instance, inside For Each file loop for this event
'and delegate it to this method

        'If we didn't succeed, we want to know what went wrong
        If e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)

        Else
            MessageBox.Show("File upload complete!")
        End If
    End Sub

    'You also don't need this for now, so just comment it for later use, when you decide what you want to do on Progress changed event
    'and here we listen to WebClient's event when UL progress changes
    'Private Sub myFtpUploadWebClient_UploadProgressChanged(ByVal sender As Object, ByVal e As System.Net.UploadProgressChangedEventArgs) Handles myFtpUploadWebClient.UploadProgressChanged
        'ProgressBar1.Value = e.ProgressPercentage
    'End Sub


Was This Post Helpful? 1
  • +
  • -

#26 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 10:50 AM

View Postlucky3, on 03 September 2012 - 10:08 AM, said:

View PostBluezap, on 03 September 2012 - 09:58 AM, said:

Like this?
'You don't need this, because you were having Dim myFtpUploadWebClient As New WebClient in 
'For Each file... loop in your previous post
'Private WithEvents myFtpUploadWebClient As New WebClient


    'here you track what happened when upload completes
    Private Sub myFtpUploadWebClient_UploadFileCompleted(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs) 'Handles myFtpUploadWebClient.UploadFileCompleted - you can't use this, because you will AddHandler to each WebClient instance, inside For Each file loop for this event
'and delegate it to this method

        'If we didn't succeed, we want to know what went wrong
        If e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)

        Else
            MessageBox.Show("File upload complete!")
        End If
    End Sub

    'You also don't need this for now, so just comment it for later use, when you decide what you want to do on Progress changed event
    'and here we listen to WebClient's event when UL progress changes
    'Private Sub myFtpUploadWebClient_UploadProgressChanged(ByVal sender As Object, ByVal e As System.Net.UploadProgressChangedEventArgs) Handles myFtpUploadWebClient.UploadProgressChanged
        'ProgressBar1.Value = e.ProgressPercentage
    'End Sub


Tried this but had no luck :(
 AddHandler myFtpUploadWebClient.UploadProgressChanged, AddressOf myFtpUploadWebClient_ProgressChanged


Sub WebClient_ProgressChanged(sender As Object, e As uploadProgressChangedEventArgs)
 'Update ProgressBar
    End Sub


Was This Post Helpful? 0
  • +
  • -

#27 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 11:29 AM

View PostBluezap, on 03 September 2012 - 10:50 AM, said:

Tried this but had no luck :(
 AddHandler myFtpUploadWebClient.UploadProgressChanged, AddressOf myFtpUploadWebClient_ProgressChanged


'You don't need this
'Sub WebClient_ProgressChanged(sender As Object, e As uploadProgressChangedEventArgs)
 'Update ProgressBar
   ' End Sub



Where did you put AddHandler, because it is written as it should be, if you left what I suggested:
 Private Sub myFtpUploadWebClient_UploadFileCompleted(sender As Object, e As System.Net.UploadFileCompletedEventArgs) 'Handles myFtpUploadWebClient.UploadFileCompleted
'messagebox to inform
End Sub


?

Delegate signature and method handling event, which delegate points to, must be the same.

I'll give you an example in console application:
Module Module1

    Sub Main()
        Dim myInstanceOfExampleClass As New ExampleClass("Class Instance #1")
        AddHandler myInstanceOfExampleClass.MyStatusUpdate, AddressOf StatusUpdate
        Console.ReadLine()
    End Sub

    Private Sub StatusUpdate(ByVal status As String)
        Console.WriteLine(status)
    End Sub
End Module

Public Class ExampleClass
    'this is what class will shout to any other object, that is willing to listen to
    Public Event MyStatusUpdate(ByVal Status As String)

    Private name As String
    Private counter As Integer = 0
    Private WithEvents myInternalTimer As Timers.Timer

    'constructor
    Public Sub New(ByVal myName As String)
        name = myName
        myInternalTimer = New Timers.Timer With {.Interval = 2000, .Enabled = True}
        myInternalTimer.Start()
    End Sub

    Private Sub myInternalTimer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles myInternalTimer.Elapsed
        counter += 1
        If counter <= 10 Then
            RaiseEvent MyStatusUpdate("My name is: " & name & ", and I'm of type: " & Me.GetType.ToString & ". I raised my voice " & counter & " times.")
        Else
            RaiseEvent MyStatusUpdate("My name is: " & name & ", and I died. Goodbye! :-((( ")
            myInternalTimer.Stop()
        End If
        
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#28 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 11:56 AM

View Postlucky3, on 03 September 2012 - 11:29 AM, said:

View PostBluezap, on 03 September 2012 - 10:50 AM, said:

Tried this but had no luck :(
 AddHandler myFtpUploadWebClient.UploadProgressChanged, AddressOf myFtpUploadWebClient_ProgressChanged



Where did you put AddHandler, because it is written as it should be

I don't seem to understand what you mean?
Where do i have to put addhandler?
Was This Post Helpful? 0
  • +
  • -

#29 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 12:04 PM

Ok i got it to show progress for each file by using a different way
Private WithEvents myFtpUploadWebClient As New WebClient


 Private Sub myFtpUploadWebClient_UploadProgressChanged(ByVal sender As Object, ByVal e As System.Net.UploadProgressChangedEventArgs) Handles myFtpUploadWebClient.UploadProgressChanged
        MbProgressBar1.Value = e.ProgressPercentage
    End Sub


 Dim myFtpUploadWebClient As New WebClient
                    AddHandler myFtpUploadWebClient.UploadProgressChanged, AddressOf myFtpUploadWebClient_UploadProgressChanged
                    Dim myUri As New Uri("ftp://testing.com/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N))
                    myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential("usrname", "pass")

                    'Now you do the uploading
                    myFtpUploadWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))


Was This Post Helpful? 0
  • +
  • -

#30 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 03 September 2012 - 12:11 PM

Bluezap I'm sorry, I missed that you posted handler for progress changed event. You should add File Completed event handler. Where? Somewhere in scope of each new instance of myFtpUploadWebClient (translation: in For Each file... loop).
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4