Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

FTP Files and Save Locally

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
bassman592
Asked:
bassman592
1 Solution
 
shanejhCommented:
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
 
bassman592Author Commented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now