【问题标题】:Upload multiple files to FTP server using vb.net使用 vb.net 将多个文件上传到 FTP 服务器
【发布时间】:2011-10-06 12:48:48
【问题描述】:

我可以上传单个文件,现在如何将多个文件上传到 FTP 服务器:

这是我正在使用的代码:

Private Sub uploadFile(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) '创建 FTP 请求

    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

        'Load the file
        Dim stream As FileStream = File.OpenRead(filePath)
        Dim buffer As Byte() = New Byte(CInt(stream.Length - 1)) {}

        stream.Read(buffer, 0, buffer.Length)
        stream.Close()

        'Upload file
        Dim reqStream As Stream = request.GetRequestStream()
        reqStream.Write(buffer, 0, buffer.Length)
        reqStream.Close()

        MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
    Catch
        MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
    End Try
End Sub


Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    btnUpload.Enabled = False
    Application.DoEvents()
    uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
    btnUpload.Enabled = True
End Sub

这是我修改但不工作的方式:

 If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim f As New IO.DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)
        For Each file As IO.FileInfo In f.GetFiles
            Select Case file.Extension.ToLower
                Case ".jpg", ".bmp", ".gif", ".png", ".ico"
                    CheckedListBox1.Items.Add(file.FullName, CheckState.Checked)
            End Select
        Next
        For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
            btnUpload.Enabled = False
            Application.DoEvents()
            uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
            btnUpload.Enabled = True
        Next
    End If
End Sub

【问题讨论】:

    标签: vb.net upload ftp


    【解决方案1】:

    根据您收集文件列表的方式,只需遍历一个集合即可。

    为了举例,我假设一个字符串:

    Dim files As List(Of String) = New List(Of String)
    
    For Each file In files
      uploadFile(txtFTPAddress.Text, file, txtUsername.Text, txtPassword.Text)
    Next
    

    另外,请考虑实现IDisposable 的对象的using 语句。

     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
    

    【讨论】:

      【解决方案2】:
      For Each _____ in ______ collection
      uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text)
      Next
      

      (填充空白取决于您用来存储文件名的控件。)

      【讨论】:

      • 当我每次上传时,我收到文件上传消息,而当它没有被上传时,我已经给出了异常消息你能说如何修改,以便当所有图片上传时我需要得到“上传”,如果没有完成,它应该给我一条消息“上传失败”
      • 有几种方法可以做到这一点,但您可以尝试的一种方法是将 for-each 循环放在 try-catch 块中。在 Catch 部分,您可以决定是退出循环并显示错误消息,还是直接跳过该文件并记录错误,或者您认为最合适的任何内容。至于成功信息,我就去掉它,或者把它放在 try-catch 块的 finally 部分。
      • 是的,我以前做过,但是这个过程需要很长时间,但是如果我不将 for each 循环保留在 try catch 块中,则上传所有内容需要几秒钟。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 2011-03-14
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多