Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FTP Files and Save Locally

Posted on 2012-04-03
2
Medium Priority
?
585 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
shanejh earned 2000 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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

660 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