0

I am attempting to load files up to an FTP server. Here is my code:

                // Create the request.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(appSettingsFTP.ftpUrl + @"/" + filename);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Timeout = 6000000; //set to 100 minutes

                // Add the login credentials.
                request.Credentials = new NetworkCredential(appSettingsFTP.ftpLogin, appSettingsFTP.ftpPassword);

                // Grab the file contents.
                StreamReader sourceStream = new StreamReader(appSettingsFTP.uploadFileDirectory + filename);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;

                // Copy the file contents to the outgoing stream.
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                LoggerFTP.Log(filename.ToString() + " " + "Upload Complete, Status: " + response.StatusCode, false);

Everything seems to work fine except for with one of the files (there are 8). The file that does not work is the largest and is roughly 160,000 kB. The error I am getting is this:

                System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
                     at System.String.ToCharArray()
                     at System.Text.Encoding.GetBytes(String s)
                     at CPMainSpringAPIExportsSC.UploadFTP.FTPUploadMethod(String viewname, String filename)

I am fairly certain the error is happening at this line in the code:

byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

Is there a way to read from the steamreader in a loop rather than all at once? I think that will fix my problem, but I can't think of how to do it.

Also, I tried to do something using the += operator, but then realized that a byte[] is static.

Any other ideas? Thanks in advance for any help.

Ian Best
  • 510
  • 1
  • 11
  • 23

3 Answers3

0

i guess its a web-application with a file-uploadcontrol

then i guess that should solve the problem, add this to your web.config:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>

"xxx" is in KB

Postback
  • 619
  • 2
  • 9
  • 27
0

160 MB worth of file shouldn't cause an OutOfMemoryException. Does this happen even when you try to load the big file in isolation (instead of uploading all 8 files at once)? Sorry I couldn't put this in a comment. Don't have comment rights.

Jordan Wilcken
  • 316
  • 2
  • 10
0

A similar answer was posted by me here. Basically you need to read the file in chunks and upload it to the server.

Community
  • 1
  • 1