Upload Multiple files to a ftp server

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

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

#46 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 09 September 2012 - 07:39 AM

View Postlucky3, on 09 September 2012 - 02:50 AM, said:

Now, instead of using WebClient on button clicked event handling routine, in For each loop, you can use MyAdvancedWebClient, and assign OpenFileDialog1.SafeFileNames(N) value to its FileName property. FileName can later, when progress changed event is raised, be easily accessed inside sender.

Now you have all the information you need, to know what file uploading MyAdvancedWebClient raised events, and update just the right listview items subitem. Good luck!

You have given me all this info and i still cant get it work properly :( this is very frustrating.
This is my pathetic attempt :/
  Private Class MyAdvancedWebClient
        Inherits WebClient

        Public Property FileName As String
            Get
                Return myFileName
            End Get
            Set(ByVal value As String)
                myFileName = value
            End Set
        End Property

        Private myFileName As String
    End Class


    Private Sub MyAdvancedWebClient_UploadProgressChanged(ByVal sender As Object, ByVal e As System.Net.UploadProgressChangedEventArgs)
        Dim n As Integer = 0
        For Each myFileName In OpenFileDialog1.FileNames

            Dim itm As New ListViewItem With {.Text = OpenFileDialog1.SafeFileNames(N)}

            itm.SubItems.Add(e.ProgressPercentage)

            ListView1.Items.Add(itm)

            n = n + 1
        Next myFileName

    End Sub


    Private Sub BrowseBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseBut.Click

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

                    Dim MyAdvancedWebClient As New MyAdvancedWebClient
                    AddHandler MyAdvancedWebClient.UploadProgressChanged, AddressOf MyAdvancedWebClient_UploadProgressChanged
                    AddHandler MyAdvancedWebClient.UploadFileCompleted, AddressOf MyAdvancedWebClient_UploadFileCompleted
                    Dim myUri As New Uri("ftp://testingcom/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N))
                    MyAdvancedWebClient.Credentials = New System.Net.NetworkCredential("", "")

                    'Now you do the uploading
                    MyAdvancedWebClient.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: " & myFileName.Substring(myFileName.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 myFileName


        End If


I know it makes absolutely no scene at all but at this point i am completely lost.
Was This Post Helpful? 0
  • +
  • -

#47 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 746
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 09 September 2012 - 07:56 AM

You should be adding item to the listview in BrowseBut_Click method, after you create new instance of MyAdvancedWebClient. There you also need to asign OpenFileDialog1.SafeFileNames(N) value to FileName property of your newly created MyAdvancedWebClient object instance. Do that before you start uploading. Do that first, than start thinking about progress changed routine.
Was This Post Helpful? 1
  • +
  • -

#48 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 09 September 2012 - 08:09 AM

Like this i suppose?
For Each myFileName In OpenFileDialog1.FileNames
                Try
                     Dim MyAdvancedWebClient As WebClient = New MyAdvancedWebClient
  Dim filename As String
                    FileName = OpenFileDialog1.SafeFileNames(N)
                    Dim itm As New ListViewItem With {.Text = OpenFileDialog1.SafeFileNames(N)}
                    ListView1.Items.Add(itm)
                    AddHandler MyAdvancedWebClient.UploadProgressChanged, AddressOf MyAdvancedWebClient_UploadProgressChanged
                    AddHandler MyAdvancedWebClient.UploadFileCompleted, AddressOf MyAdvancedWebClient_UploadFileCompleted
                    Dim myUri As New Uri("ftp://testing.com/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N))
                    MyAdvancedWebClient.Credentials = New System.Net.NetworkCredential("", "")

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


This post has been edited by Bluezap: 09 September 2012 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#49 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 746
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 09 September 2012 - 08:51 AM

I know, it's too much new information for you right now. Let's put an end to your struggles :)

View PostBluezap, on 09 September 2012 - 08:09 AM, said:

 
'I hope this For Each loop of your code was from BrowseBut_Click sub method 
For Each myFileName In OpenFileDialog1.FileNames 
                Try 
                     'if you define it as WebClient, you won't have access to FileName property! 
                     'it would be also smart, if you use instance names that differ from class names, so you can know at any point in code, with what you are dealing with. Here you could have Dim myInstanceOfMyAdvancedWebClient As... or something similar. 
                     'Dim MyAdvancedWebClient As WebClient = New MyAdvancedWebClient <-- look at the comment 2 lines above 
Dim MyAdvancedWebClient As New MyAdvancedWebClient 
'Dim filename As String <-- you don't need this 
 
'you should assign OpenFileDialog1.SafeFileNames(N) value to MyAdvancedWebClient.FileName property, not to some newly created filename as string (as you did in previous line of code)  
MyAdvancedWebClient.FileName = OpenFileDialog1.SafeFileNames(N) 
Dim itm As New ListViewItem With {.Text = OpenFileDialog1.SafeFileNames(N)} 
 
'you forgot to add subitem 
itm.SubItems.Add("0%") 
ListView1.Items.Add(itm) 
AddHandler MyAdvancedWebClient.UploadProgressChanged, AddressOf MyAdvancedWebClient_UploadProgressChanged 
AddHandler MyAdvancedWebClient.UploadFileCompleted, AddressOf MyAdvancedWebClient_UploadFileCompleted 
Dim myUri As New Uri("ftp://testing.com/public_html/hello123/" & OpenFileDialog1.SafeFileNames(N)) 
MyAdvancedWebClient.Credentials = New System.Net.NetworkCredential("", "") 
 
'Now you do the uploading 
MyAdvancedWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N)) 



Now you have MyAdvancedWebClient with FileName property, that will be accessible in progress changed event handling method.

What is left, is simple comparison of each listview item's subitem value to FileName property of event rising object.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#50 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 10 September 2012 - 06:12 AM

THANK YOU SOOOOOO MUCHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! i promise that i wont just copy and paste the whole thing, I will learn the coding properly first :)
KUDOS TO ALL YOUR POSTS!
Was This Post Helpful? 1
  • +
  • -

#51 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 746
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 10 September 2012 - 07:28 AM

Thank you Bluezap for all your pluses. I'm sure you have learned a thing or two from this thread, and if you are really willing to learn VB.NET, start with basics. As tlhIn`toq often says, pick one of "Learn VB.NET in 30 days" beginners books, and work it out cover to cover. When you are able to really understand what each line of code is doing on simple tasks, you can do that on more complex things later, too.
Was This Post Helpful? 0
  • +
  • -

#52 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 10 September 2012 - 09:45 AM

Thanks for the advice, I will look into this :)
Was This Post Helpful? 0
  • +
  • -

#53 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 23 September 2012 - 03:26 AM

Hey i just wanted to ask if this is possible -
When i click an item on the listview control i need the upload url of the file to appear on a label.
To get the url i will use the code
 Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

Label.Text = "http://" + "testing.com" + "/" + (What do i add here?)



Was This Post Helpful? 0
  • +
  • -

#54 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 746
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 23 September 2012 - 05:36 AM

Listview1.SelectedValue.ToString
Was This Post Helpful? 0
  • +
  • -

#55 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 23 September 2012 - 07:56 AM

View Postlucky3, on 23 September 2012 - 05:36 AM, said:

Listview1.SelectedValue.ToString

SelectedValue is not a member of Listview, i tried -
  Label1.Text = "http://" + "testing.com" + "/" + ListView1.SelectedItems.ToString


Doesn't seem to work
Was This Post Helpful? 0
  • +
  • -

#56 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 746
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 23 September 2012 - 08:28 AM

What I suggested before was for ListView web control. For form control, you can simply display text property of item on index 0 in SelectedItems collection: Label1.Text = ListView1.SelectedItems(0).Text.
Was This Post Helpful? 1
  • +
  • -

#57 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 23 September 2012 - 09:16 AM

That's quite right but the problem is that when i click on another item in the listview the program crashes.Basically i want the label to show the upload url of the item that i have clicked on.
I tried this -
 Dim x As Integer
        x = ListView1.FocusedItem.Index
        Label1.Text = "http://" + "testing.com" + "/" + ListView1.SelectedItems(x).Text


But still the same error.
Was This Post Helpful? 0
  • +
  • -

#58 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 746
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 23 September 2012 - 09:45 AM

You'll find your answer, why the code below works, if you read how ListView.SelectedIndexChanged event behaves.

        If ListView1.SelectedItems.Count > 0 Then
            Label1.Text = ListView1.SelectedItems(0).Text
        End If



Edit: changed url to the right MSDN page.

This post has been edited by lucky3: 23 September 2012 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#59 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 23 January 2013 - 04:32 AM

I have been playing around with the code a bit but i haven't been able to figure an important part out.
When the files are added to the ListView is there any way of adding a context menu and having an option to cancel the upload process?
I know you can cancel the upload by using
MyAdvancedWebClient.CancelAsync()

but how do i connect that to the index of the file that i am right clicking?
Was This Post Helpful? 0
  • +
  • -

#60 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 23 January 2013 - 11:04 AM

any one know how i can CancelAsync a single file in the listview?

This post has been edited by Bluezap: 23 January 2013 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4