【问题标题】:Upload directory and its sub directories and contents using VB.NET over FTP使用 VB.NET 通过 FTP 上传目录及其子目录和内容
【发布时间】:2015-06-21 09:43:10
【问题描述】:

我想制作一个 VB.NET 软件,我们可以在其中选择文件夹并将它们添加到列表框中。这些文件夹及其内容将在一定时间上传到 FTP 站点。我的问题是将文件夹及其内容上传到 FTP 站点的代码是什么。列表框仅包含主目录位置。列表框中可能有多个目录。按钮 3 是 uploadnow 按钮,稍后我将连接到计时器。按钮 2 用于选择目录。

到目前为止,我已经做到了:

Imports System.IO

Public Class SYNC

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim dialog = New FolderBrowserDialog()
        Dim dir As String
        dialog.SelectedPath = Application.StartupPath
        If DialogResult.OK = dialog.ShowDialog() Then
            dir = dialog.SelectedPath
            ListBox1.Items.Add(Path.GetFileName(dir))
        End If

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    End Sub

End Class

【问题讨论】:

  • 有人请回复这对我来说很重要。谢谢

标签: vb.net file-upload ftp


【解决方案1】:

WinSCP .NET assembly 内置递归传输。

使用Session.PutFiles method 喜欢:

' Setup session options
Dim mySessionOptions As New SessionOptions
With mySessionOptions
    .Protocol = Protocol.Ftp
    .HostName = "example.com"
    .UserName = "user"
    .Password = "mypassword"
End With

Using mySession As Session = New Session
    ' Connect
    mySession.Open(mySessionOptions)

    ' Upload files
    mySession.PutFiles("d:\foldertoupload\*", "/home/user/").Check()
End Using

查看full example


请注意,Session.PutFiles(以及整个程序集)具有同步接口(调用是阻塞的)。所以你需要从一个单独的线程执行代码,而不是从 GUI 线程。否则,您的界面将在传输过程中无响应。

要向用户提供进度反馈,请使用Session.FileTransferProgress eventFileTransferProgressEventArgs.OverallProgress property

(我是 WinSCP 的作者)

【讨论】:

  • 所以这段代码会将所有内容和子目录上传到ftp?顺便说一句,非常感谢您的帮助
  • 还有一个选项可以设置上传或同步所选文件夹的特定时间吗?
  • 是的,它将递归上传文件夹。对于完全不同的问题,安排上传是完全不同的任务。如果您要发布一个,请确保您指定是否应该在您的应用程序仍在运行时进行上传;或在未来的任何时间。
  • 是的,调度不是问题,我可以使用计时器,我有这个想法。还有一个帮助,我如何为这段代码使用进度条?请帮我解决这个问题
  • 非常感谢,我会试一试。再次非常感谢
【解决方案2】:

您需要使用System.Net.FtpWebRequest 类。

这是我在这里找到的一个未经我测试的示例(修改为使用Using 语句): http://www.digitalcoding.com/Code-Snippets/VB/Visual-Basic-Code-Snippet-Upload-file-to-FTP-Server.html


''' <summary>
''' Methods to upload file to FTP Server
''' </summary>
''' <param name="_FileName">local source file name</param>
''' <param name="_UploadPath">Upload FTP path including Host name</param>
''' <param name="_FTPUser">FTP login username</param>
''' <param name="_FTPPass">FTP login password</param>
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
    Dim _FileInfo As New System.IO.FileInfo(_FileName)

    ' Create FtpWebRequest object from the Uri provided
    Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)

    ' Provide the WebPermission Credintials
    _FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)

    ' By default KeepAlive is true, where the control connection is not closed
    ' after a command is executed.
    _FtpWebRequest.KeepAlive = False

    ' set timeout for 20 seconds
    _FtpWebRequest.Timeout = 20000

    ' Specify the command to be executed.
    _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

    ' Specify the data transfer type.
    _FtpWebRequest.UseBinary = True

    ' Notify the server about the size of the uploaded file
    _FtpWebRequest.ContentLength = _FileInfo.Length

    ' The buffer size is set to 2kb
    Dim buffLength As Integer = 2048
    Dim buff(buffLength - 1) As Byte

    ' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
    Using _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

        Try
            ' Stream to which the file to be upload is written
            Using _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
                ' Read from the file stream 2kb at a time
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

                ' Till Stream content ends
                Do While contentLen <> 0
                    ' Write Content from the file stream to the FTP Upload Stream
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)
                Loop

                ' Close the file stream and the Request Stream
                _Stream.Close()
                _Stream.Dispose()
                _FileStream.Close()
                _FileStream.Dispose()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Using

End Sub

【讨论】:

  • 但这不只是用于文件上传吗?我需要在 ftp 上上传目录及其子目录以及其中的所有文件
猜你喜欢
  • 2012-10-21
  • 2021-08-07
  • 2023-03-12
  • 2013-04-30
  • 1970-01-01
  • 2011-05-05
  • 2020-04-23
  • 1970-01-01
  • 2010-10-29
相关资源
最近更新 更多