如何使用vb.net 2008从ftp服务器下载文件 [英] how to download file from ftp server using vb.net 2008
问题描述
hi
我曾使用过以下代码,但问题是copyto功能不支持ms 2008 ..它只适用于ms 2010
任何人都可以帮助我? for this
Dim ftp As < span class =code-keyword> String = ftp://xxxxxxxxxx.com/
' FTP文件夹名称。如果要从根文件夹下载文件,请留空。
Dim ftpFolder As 字符串 = downloads /
尝试
' 创建FTP请求。
Dim request As FtpWebRequest = DirectCast (WebRequest.Create(Convert.ToString(ftp& ftpFolder)& fileName),FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
' 输入FTP服务器凭证。
request.Credentials = 新 NetworkCredential( xxxxxxx, xxxxxxx)
request.UsePassive = True
request.UseBinary = True
request.EnableSsl = False
' 获取响应并将其读入MemoryStream对象。
Dim resp As FtpWebResponse = DirectCast (request.GetResponse(),FtpWebResponse)
使用 stream 作为 新 MemoryStream()
' 下载文件。
resp.GetResponseStream()。Copyto(stream)
Response.AddHeader( content-disposition, attachment; filename =& fileName)
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(stream.ToArray())
响应。结束()
结束 使用
Catch ex As WebException
投掷 新异常( TryCast (ex.Response,FtpWebResponse).StatusDescription)
End 尝试
[edit}删除了FTP地址和凭据[ / edit]
提供自己版本的CopyTo
方法相当简单:
C#:
使用系统;
使用 System.IO;
public static class StreamExtensions
{
public static void CopyTo(此流源,流目的地)
{
if (source == null ) throw 新 ArgumentNullException( source);
if (destination == null ) throw new ArgumentNullException( destination跨度>);
if (!source.CanRead) throw new NotSupportedException( 无法从源流中读取。);
if (!destination.CanWrite) throw new NotSupportedException( 无法写入目标流。);
int bytesRead;
byte [] buffer = new byte [ 81920 ];
while ((bytesRead = source.Read(buffer, 0 ,buffer.Length)) != 0 )
{
destination.Write(buffer, 0 ,bytesRead );
}
}
}
VB.NET:
< pre lang =vb.net> Imports System.IO
Imports System.Runtime。 CompilerServices
公共 模块 StreamExtensions
< Extension> _
公开 Sub CopyTo( ByVal source As Stream, ByVal destination As 流)
如果来源 什么 然后
投掷 新 ArgumentNullException( source)
结束 如果
如果目的地是 没什么 然后
投掷 新 ArgumentNullException( destination)
结束 如果
如果 不 source.CanRead 然后
投掷 新 NotSupportedException( 无法从源流中读取。)
结束 如果
如果 不 destination.CanWrite 那么
抛出 新 NotSupportedException( 不能写给目标流。)
结束 如果
Dim buffer As Byte () = 新 字节( 81919 ){}
Dim bytesRead As 整数 = source.Read(buffer, 0 ,buffer.Length)
while bytesRead<> ; 0
destination.Write(buffer, 0 ,bytesRead)
bytesRead = source .Read(buffer, 0 ,buffer.Length)
结束 while
结束 Sub
结束 模块
hi
I HAD used this below code but the issue is the copyto function are not supporting to ms 2008 .. it only work in ms 2010
can anybody help me ?? for this
Dim ftp As String = "ftp://xxxxxxxxxx.com/"
'FTP Folder name. Leave blank if you want to Download file from root folder.
Dim ftpFolder As String = "downloads/"
Try
'Create FTP Request.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(Convert.ToString(ftp & ftpFolder) & fileName), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
'Enter FTP Server credentials.
request.Credentials = New NetworkCredential("xxxxxxx", "xxxxxxx")
request.UsePassive = True
request.UseBinary = True
request.EnableSsl = False
'Fetch the Response and read it into a MemoryStream object.
Dim resp As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Using stream As New MemoryStream()
'Download the File.
resp.GetResponseStream().Copyto(stream)
Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(stream.ToArray())
Response.End()
End Using
Catch ex As WebException
Throw New Exception(TryCast(ex.Response, FtpWebResponse).StatusDescription)
End Try
[edit}Removed FTP address and credentials[/edit]
It's fairly simple to provide your own version of theCopyTo
method:
C#:
using System; using System.IO; public static class StreamExtensions { public static void CopyTo(this Stream source, Stream destination) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); if (!source.CanRead) throw new NotSupportedException("Cannot read from the source stream."); if (!destination.CanWrite) throw new NotSupportedException("Cannot write to the destination stream."); int bytesRead; byte[] buffer = new byte[81920]; while ((bytesRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, bytesRead); } } }
VB.NET:
Imports System.IO Imports System.Runtime.CompilerServices Public Module StreamExtensions <Extension> _ Public Sub CopyTo(ByVal source As Stream, ByVal destination As Stream) If source Is Nothing Then Throw New ArgumentNullException("source") End If If destination Is Nothing Then Throw New ArgumentNullException("destination") End If If Not source.CanRead Then Throw New NotSupportedException("Cannot read from the source stream.") End If If Not destination.CanWrite Then Throw New NotSupportedException("Cannot write to the destination stream.") End If Dim buffer As Byte() = New Byte(81919) {} Dim bytesRead As Integer = source.Read(buffer, 0, buffer.Length) While bytesRead <> 0 destination.Write(buffer, 0, bytesRead) bytesRead = source.Read(buffer, 0, buffer.Length) End While End Sub End Module
这篇关于如何使用vb.net 2008从ftp服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!