0

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

1 Answers1

0

Since uploadFile is called each time the file is loaded, you may want to move the error handling code from this method to the dragDrop method. This way, you will receive only one message saying that the entire operation succeeded or failed. You could also set the Timeout property on the FtpWebRequest to cancel the upload if it takes more than a few seconds.

Then uploadFile becomes:

Private Sub uploadFile(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) 'Create FTP request
    Dim request as FtpWebRequest = ...
    request.Timeout = 5000 ' Set timeout to 5 seconds
    '... several lines omitted
    'Upload file
    Using reqStream As Stream = request.GetRequestStream()
        reqStream.Write(buffer, 0, buffer.Length)
    End Using
End Sub

You could then move the error handling code into the method handling the drag/drop:

Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As  
    System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
    Try
       ' ... several lines omitted
       Next
       MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
    Catch
       MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
    End Try
End Sub
drf
  • 8,461
  • 32
  • 50
  • It is giving me message as "Uploaded Successfully" but not uploaded to the FTP. –  Jul 30 '11 at 11:04
  • That's strange...did you completely remove the try/catch/end try lines from your uploadFile method? – drf Jul 30 '11 at 11:23
  • yes I have done it but takes long time for each file and gets the same message –  Jul 30 '11 at 12:01
  • If I remove request.Timeout = 5000 ' Set timeout to 5 seconds this line then it works but it takes so much of time to upload and is there any other way to upload quickly. –  Jul 30 '11 at 12:08
  • Someone asked a similar question about this at http://stackoverflow.com/questions/1016690/how-to-improve-the-performance-of-ftpwebrequest, and there are a number of responses there that that might help you. – drf Jul 30 '11 at 12:28