I am trying to add multiple files to FTP server by dragging and dropping and I am able to do that using try catch block and if we give the ftp settings correctly it takes 1 sec to upload them but when we give wrong details it hangs up and dosen't give me any error message though If I give an exceptional message.
Now I am getting error message as well as success message for every file I add.I do not want that to be happen.
Can any one say me where should I give messages for success and failure so that it should take few seconds for uploading and if not should give me a message immediately.
I am totally confused where I am going wrong.
Any help will be greatly appreciated!
Here is my code:
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
Here is the code for drag and drop
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Try
Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each FileName As String In Files
Dim Extension As String = Path.GetExtension(FileName).ToLower
If Array.IndexOf(SupportedExtensions, Extension) <> -1 Then
uploadFile(txtFTPAddress.Text, FileName, txtUsername.Text, txtPassword.Text)
End If
Next
Catch
End Try
End Sub