Visual Basic FtpWebRequest下载文件? [英] Visual Basic FtpWebRequest downloading files?

查看:158
本文介绍了Visual Basic FtpWebRequest下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

  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(在此处插入用户名,在此处插入密码)

我试图将 .txt 文件下载到我的 c:驱动器。我已经有一个连接,那么如何保存 .txt 文件?另外,我怎样才能上传文件?我已经尝试了 My.Computer.Network.DownloadFile ,但只能下载/上传一次,因为我不知道如何摆脱这种连接。 / p>

解决方案

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


$ b $ pre $ 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 不提供,请使用 的FtpWebRequest 。简单的方法是使用 FileStream .stream.copyto.aspxrel =nofollow noreferrer> 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

使用ftpStream As Stream = request.GetResponse()。GetResponseStream(),
fileStream As Stream = File.Create(C:\local\path\file.zip)
ftpStream.CopyTo(fileStream)
结束使用






  Dim request As FtpWebRequest = 
WebRequest.Create(ftp://ftp.example.com/remote/path/file.zip)
request.Credentials =新建NetworkC (),
使用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)
如果读取> 0然后
fileStream.Write(buffer,0,read)
Console.WriteLine(Downloaded {0} bytes,fileStream.Position)
End If
Loop While read> ; 0
结束使用

对于GUI进度(WinForms ProgressBar ),请参阅(C#):

FtpWebRequest FTP与ProgressBar下载 p>

如果要从远程文件夹下载所有文件,请参阅

如何使用VB.NET从FTP下载目录


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.

解决方案

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

这篇关于Visual Basic FtpWebRequest下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆