Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to download a file via FTP using sample code I found as my old code doesn't work with larger files due to it grabbing the whole file at once which causes Out of Memory errors.

The reader.read() is asking for (char[],int,int) i'm feeding it (byte[],int,int) like normal Read() methods I have used in the past.

Is it asking for char[] because it's a response stream? and How do I even create a char[] buffer that is simular to the byte[] I have used previously?

Am I even on the right track?


FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp01/temp.exe");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential("admin", "1234");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            FileStream file = File.Create(@"c:\temp\temp.exe");
            byte[] buffer = new byte[32 * 1024];
            int read;
            reader.Read(
            while ((read = reader.Read(buffer,0,buffer.Length)) > 0)
            {
                file.Write(buffer, 0, read);
            }
            file.Close();
            reader.Close();
            response.Close();



Thanks in advance for any help.
Posted
Updated 8-Jul-10 7:07am
v2
Comments
TheyCallMeMrJames 10-Jul-10 12:04pm    
Did you try removing the StreamReader (per the revised code below)?

1 solution

I don't think you're too far off. Other than the incomplete line, I don't see much that won't work there. Are you getting an error?

Here's[^] some code very close to what you're doing (halfway down).

Edit:
Try this instead...don't go with a StreamReader, just use the Stream.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp01/temp.exe");
request.Method = WebRequestMethods.Ftp.DownloadFile;

request.Credentials = new NetworkCredential("admin", "1234");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
FileStream file = File.Create(@"c:\temp\temp.exe");
byte[] buffer = new byte[32 * 1024];
int read;
//reader.Read(

while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    file.Write(buffer, 0, read);
}

file.Close();
responseStream.Close();
response.Close();


Cheers.
 
Share this answer
 
v3
Comments
astroudjr 8-Jul-10 12:08pm    
It will not even let me run it like this, my issues are listed above the code.
TheyCallMeMrJames 8-Jul-10 12:34pm    
Updated and removed the use of the StreamReader. hth, Cheers.

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