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.