Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 Folder name. Leave blank if you want to Download file from root folder.
        Dim ftpFolder As String = "downloads/"

            '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.
                Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
            End Using
        Catch ex As WebException
            Throw New Exception(TryCast(ex.Response, FtpWebResponse).StatusDescription)
        End Try

[edit}Removed FTP address and credentials[/edit]
Updated 24-Jun-15 3:19am

1 solution

It's fairly simple to provide your own version of the CopyTo method:

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

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
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900