【问题标题】:Visual Basic FtpWebRequest downloading files?Visual Basic FtpWebRequest 下载文件?
【发布时间】:2012-12-07 21:26:06
【问题描述】:

我有什么:

Dim ftploader As System.Net.FtpWebRequest =
    DirectCast(System.Net.WebRequest.Create(
        "ftp://ftp.cabbageee.host-ed.me/nim/Vardelatestmessage.txt"),
        System.Net.FtpWebRequest)

ftploader.Credentials =
    New System.Net.NetworkCredential("Insert Username here", "Insert password here")

我正在尝试将此.txt 文件下载到我的c: 驱动器。我已经建立了连接,那么如何保存 .txt 文件?另外,如何上传文件?我已经尝试过My.Computer.Network.DownloadFile,但只能下载/上传一次,因为我不知道如何摆脱这种连接。

【问题讨论】:

    标签: .net vb.net ftp ftpwebrequest


    【解决方案1】:

    使用 VB.NET 从 FTP 服务器下载二进制文件最简单的方法是使用WebClient.DownloadFile

    Dim client As WebClient = New WebClient()
    client.Credentials = New NetworkCredential("username", "password")
    client.DownloadFile(
        "ftp://ftp.example.com/remote/path/file.zip", "C:\local\path\file.zip")
    

    如果您需要WebClient 不提供的更大控制(如TLS/SSL encryption、ascii/文本传输模式、resuming transfers 等),请使用FtpWebRequest。简单的方法是使用 Stream.CopyTo 将 FTP 响应流复制到 FileStream

    Dim request As FtpWebRequest =
        WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
    request.Credentials = New NetworkCredential("username", "password")
    request.Method = WebRequestMethods.Ftp.DownloadFile
    
    Using ftpStream As Stream = request.GetResponse().GetResponseStream(),
          fileStream As Stream = File.Create("C:\local\path\file.zip")
        ftpStream.CopyTo(fileStream)
    End Using
    

    如果你需要监控一个下载进度,你必须自己分块复制内容:

    Dim request As FtpWebRequest =
        WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
    request.Credentials = New NetworkCredential("username", "password")
    request.Method = WebRequestMethods.Ftp.DownloadFile
    
    Using ftpStream As Stream = request.GetResponse().GetResponseStream(),
          fileStream As Stream = File.Create("C:\local\path\file.zip")
        Dim buffer As Byte() = New Byte(10240 - 1) {}
        Dim read As Integer
        Do
            read = ftpStream.Read(buffer, 0, buffer.Length)
            If read > 0 Then
                fileStream.Write(buffer, 0, read)
                Console.WriteLine("Downloaded {0} bytes", fileStream.Position)
            End If
        Loop While read > 0
    End Using
    

    有关 GUI 进度 (WinForms ProgressBar),请参阅 (C#):
    FtpWebRequest FTP download with ProgressBar

    如果您想从远程文件夹下载所有文件,请参阅
    How to download directories from FTP using VB.NET

    【讨论】:

      【解决方案2】:

      您需要调用GetResponse,然后您就可以访问包含您的内容的响应流,然后您可以将该流写入您要保存的文本文件中。

      似乎有一个pretty well fleshed out sample here(它在 C# 中,但我认为应该很容易翻译成 VB)。

      【讨论】:

        【解决方案3】:

        试试这个:

         Dim myWebClient As New System.Net.WebClient
         Dim webfilename As String = "http://www.whatever.com/example.txt"
         Dim file As New System.IO.StreamReader(myWebClient.OpenRead(webfilename))
        
         gCurrentDataFileContents = file.ReadToEnd()
        
         file.Close()
         file.Dispose()
         myWebClient.Dispose()
        

        【讨论】:

          猜你喜欢
          • 2011-02-16
          • 2012-09-13
          • 1970-01-01
          • 2011-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-28
          相关资源
          最近更新 更多