如何使用vb.net 2008从ftp服务器下载文件 [英] how to download file from ftp server using vb.net 2008

查看:74
本文介绍了如何使用vb.net 2008从ftp服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 the CopyTo 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屋!

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