Solved

FTP Files and Save Locally

Posted on 2012-04-03
2
550 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Total in textboxes 9 34
Wpf develop 5 29
display data in text field from data base for updating 6 25
Code enhancement 5 12
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now