Solved

FTP Files and Save Locally

Posted on 2012-04-03
2
572 Views
Last Modified: 2012-04-03
I am using C# to download - via FTP - some files and save them locally (on a network). The files are zipped. The class I have developed so far does a fine job with the smaller files, but I get a "Exception of type 'System.OutOfMemoryException' was thrown." error with the larger files. By "smaller" I mean < 50MB, by "larger"  I mean 350MB or so.

Here is the download method of my FTP class:

// Download a file from the FTP server to the destination
public void Download(string file, string destination)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + file);
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                StreamWriter writer = new StreamWriter(destination);

                writer.Write(reader.ReadToEnd());  <<<<<

                writer.Close();
                reader.Close();
                response.Close();


            }
            catch(UriFormatException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

and I call it like this:

MyClass.Download("File1.zip", @"\\network.com\test\File1.zip");

I call this in a loop, and it will copy files until it hits a larger one, then I get the exception at the line:

writer.Write(reader.ReadToEnd());

The files that it does copy are fine.

I know I need to somehow stream the file transfer - it must be trying to hold the whole thing in memory - but I just started using C# so I don't really know how to go about it.

Any help is much appreciated.

**NOTE** Let me amend this - I further checked the zip files that were - supposedly - copied without an error and found that they are NOT valid zip files. They just have the correct name and the zip extension. Also, they are all a little larger than the originals (about 2/3 larger). I looked at them in a hex viewer and I can't tell what the difference is, but it's obviously significant. So I guess I have 2 problems...
0
Comment
Question by:bassman592
2 Comments
 
LVL 2

Accepted Solution

by:
shanejh earned 500 total points
ID: 37803819
You need to buffer and stream the file correctly.
The following link shows this quite nicely:
Simple FTP demo application using C#.Net 2.0
0
 

Author Closing Comment

by:bassman592
ID: 37804057
Thanks, that was perfect! It solved both of my problems plus gave me a lot of code to dig through and figure out.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question