59 Replies - 3849 Views - Last Post: 23 January 2013 - 11:04 AM
#16
Re: Upload Multiple files to a ftp server
Posted 01 September 2012 - 10:48 PM
#17
Re: Upload Multiple files to a ftp server
Posted 01 September 2012 - 10:51 PM
WebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))
still get the same error and now nothing is getting uploaded
#18
Re: Upload Multiple files to a ftp server
Posted 01 September 2012 - 11:36 PM
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
#19
Re: Upload Multiple files to a ftp server
Posted 01 September 2012 - 11:44 PM
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).
#20
Re: Upload Multiple files to a ftp server
Posted 02 September 2012 - 09:53 AM
lucky3, on 01 September 2012 - 11:44 PM, said:
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
#22
Re: Upload Multiple files to a ftp server
Posted 02 September 2012 - 10:58 PM
lucky3, on 02 September 2012 - 10:01 AM, said:
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?
#23
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 06:10 AM
Bluezap, on 02 September 2012 - 10:58 PM, said:
...
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!"
#24
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 09:58 AM
lucky3, on 03 September 2012 - 06:10 AM, said:
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
#25
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 10:08 AM
Bluezap, on 03 September 2012 - 09:58 AM, said:
'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
#26
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 10:50 AM
lucky3, on 03 September 2012 - 10:08 AM, said:
Bluezap, on 03 September 2012 - 09:58 AM, said:
'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
#27
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 11:29 AM
Bluezap, on 03 September 2012 - 10:50 AM, said:
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
#28
#29
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 12:04 PM
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))
#30
Re: Upload Multiple files to a ftp server
Posted 03 September 2012 - 12:11 PM
|
|

New Topic/Question
Reply




MultiQuote



|