Visual Basic FtpWebRequest downloading files?

我的梦境 提交于 2019-12-08 06:46:22

问题


What I have:

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")

I am trying to download this .txt file to my c: drive. I already have a connection, so how can I save that .txt file? Also, how can I upload a file? I already tried My.Computer.Network.DownloadFile, but it is only possible to download/upload once, as I have no idea of how to get rid of that connection.


回答1:


The most trivial way to download a binary file from an FTP server using VB.NET is using 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")

If you need a greater control, that WebClient does not offer, use FtpWebRequest. Easy way is to just copy an FTP response stream to FileStream using Stream.CopyTo:

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

If you need to monitor a download progress, you have to copy the contents by chunks yourself:

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

For GUI progress (WinForms ProgressBar), see (C#):
FtpWebRequest FTP download with ProgressBar

If you want to download all files from a remote folder, see
How to download directories from FTP using VB.NET




回答2:


You'll need to call GetResponse and then you'll have access to the response stream which will include your content, you could then write that stream into the text file you want to save.

There seems to be a pretty well fleshed out sample here (it's in C# but I think should be fairly easy to translate to VB).




回答3:


try this instead:

 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()


来源:https://stackoverflow.com/questions/13771222/visual-basic-ftpwebrequest-downloading-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!