【问题标题】:Problem while Uploading multiple files by drag and drop to FTP server using VB.net使用VB.net通过拖放上传多个文件到FTP服务器时出现问题
【发布时间】:2011-07-30 10:12:05
【问题描述】:

我正在尝试通过拖放将多个文件添加到 FTP 服务器,我可以使用 try catch 块来做到这一点,如果我们正确地提供了 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

【问题讨论】:

    标签: vb.net winforms error-handling ftp


    【解决方案1】:

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

    那么uploadFile就变成了:

    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 行?
    • 是的,我已经完成了,但是每个文件需要很长时间并且得到相同的消息
    • 如果我删除 request.Timeout = 5000 ' 将此行设置超时为 5 秒,那么它可以工作,但是上传需要很长时间,有没有其他方法可以快速上传。
    • 有人在stackoverflow.com/questions/1016690/… 上提出了类似的问题,那里有许多回答可能对您有所帮助。
    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 2011-11-16
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    相关资源
    最近更新 更多