使用VB.net通过拖放方式将多个文件上载到FTP服务器时出现问题

使用VB.net通过拖放方式将多个文件上载到FTP服务器时出现问题,vb.net,winforms,error-handling,ftp,Vb.net,Winforms,Error Handling,Ftp,我正在尝试通过拖放将多个文件添加到FTP服务器,我可以使用try catch block来完成此操作,如果我们正确提供FTP设置,则需要1秒时间上载这些文件,但当我们提供错误的详细信息时,它会挂断,并且不会给我任何错误消息,尽管我提供了异常消息 现在,我得到的错误消息,以及我添加的每个文件的成功消息。我不希望这种情况发生 有谁能告诉我,我应该在哪里给成功和失败的消息,以便它应该需要几秒钟上传,如果没有,应该立即给我一个消息 我完全不知道我哪里出了问题 任何帮助都将不胜感激 这是我的密码: Pri

我正在尝试通过拖放将多个文件添加到FTP服务器,我可以使用try catch block来完成此操作,如果我们正确提供FTP设置,则需要1秒时间上载这些文件,但当我们提供错误的详细信息时,它会挂断,并且不会给我任何错误消息,尽管我提供了异常消息

现在,我得到的错误消息,以及我添加的每个文件的成功消息。我不希望这种情况发生

有谁能告诉我,我应该在哪里给成功和失败的消息,以便它应该需要几秒钟上传,如果没有,应该立即给我一个消息

我完全不知道我哪里出了问题

任何帮助都将不胜感激

这是我的密码:

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
下面是拖放的代码

 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

由于每次加载文件时都会调用uploadFile,因此您可能希望将错误处理代码从此方法移动到dragDrop方法。这样,您将只收到一条消息,说明整个操作成功或失败。您还可以在FtpWebRequest上设置Timeout属性,以便在上载时间超过几秒钟时取消上载

然后,上载文件变为:

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
然后,您可以将错误处理代码移动到处理拖放的方法中:

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

它向我发送的消息已成功上载,但未上载到FTP。这很奇怪……您是否已从uploadFile方法中完全删除try/catch/end try行?是的,我已删除,但每个文件都需要很长时间,并收到相同的消息如果我删除请求。超时=5000'将超时设置为5秒,这一行可以正常工作,但上传要花很多时间,还有没有其他方法可以快速上传。有人在上问了一个类似的问题,那里有很多回答可能会对你有所帮助。