vb.net Visual Basic FtpWebRequest 下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13771222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Basic FtpWebRequest downloading files?
提问by user1869088
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 .txtfile to my c:drive. I already have a connection, so how can I save that .txtfile? 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.
我正在尝试将此.txt文件下载到我的c:驱动器。我已经建立了连接,那么如何保存该.txt文件?另外,如何上传文件?我已经尝试过了My.Computer.Network.DownloadFile,但只能下载/上传一次,因为我不知道如何摆脱该连接。
回答by Martin Prikryl
The most trivial way to download a binary file from an FTP server using VB.NET is using WebClient.DownloadFile:
使用 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")
If you need a greater control, that WebClientdoes not offer, use FtpWebRequest. Easy way is to just copy an FTP response stream to FileStreamusing Stream.CopyTo:
如果您需要更好的控制,WebClient但没有提供,请使用FtpWebRequest. 简单的方法是将 FTP 响应流复制到FileStream使用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
有关 GUI 进度 (WinForms ProgressBar),请参阅 (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
如果要从远程文件夹下载所有文件,请参阅
如何使用 VB.NET 从 FTP 下载目录
回答by Jim O'Neil
You'll need to call GetResponseand 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.
您需要调用GetResponse,然后您将可以访问包含您的内容的响应流,然后您可以将该流写入要保存的文本文件中。
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).
回答by Rob
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()

