Solved

FTP Files and Save Locally

Posted on 2012-04-03
2
555 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

15 Experts available now in Live!

Get 1:1 Help Now