1

I am sending a large file (3gb) through ftp in c# and I'm geting an error while reading the file in the Source Stream when I am doing this :

StreamReader sourceStream = new StreamReader(@"C:\xxx\xxxx\xxx"); 
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 

The error :

An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll

The entire code :

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/file.iso");
request.Method = WebRequestMethods.Ftp.UploadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("user", "mdp");

// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(@"C:\xxx\xxx\xxx\xxxxxxxxx.iso");

byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

response.Close();
Bernd Linde
  • 2,098
  • 2
  • 16
  • 22
  • 1
    You will have to send it in small amounts(Packets) but will need to put a check in recieving end to see if it has recieved it all – ZoomVirus Oct 30 '14 at 13:05
  • You read all file to memory. How many RAM you have on machine? – Renatas M. Oct 30 '14 at 13:05
  • Thank you @ZoomVirus, but do you now how can i send it ? I've never use this kind of things. – Rachid Quenelle Oct 30 '14 at 13:10
  • @Reniuz I have 16gb of RAM i do not think that the problem is from here, but thank you for your response ! – Rachid Quenelle Oct 30 '14 at 13:12
  • Have a look at this possible duplicate, where the answer explains how to receive chunks of a file [How to split a large file into chunks in c#?](http://stackoverflow.com/questions/5659189/how-to-split-a-large-file-into-chunks-in-c) – Bernd Linde Oct 30 '14 at 13:12
  • Its from there if you get out of memory on ReadToEnd line. There ara many examples how to upload large file in chunks [here](http://stackoverflow.com/questions/10151680/upload-file-on-ftp) [here](http://www.codeproject.com/Articles/17202/Simple-FTP-demo-application-using-C-Net), [here](http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class) and so on. – Renatas M. Oct 30 '14 at 13:17

1 Answers1

0

You can't read 3GB file into memory buffer. You have to do streamed transfer. Use file stream to read into a predefined buffer and then write that to ftp stream. Continue until you reach end of the file stream.

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/file.iso");
request.Method = WebRequestMethods.Ftp.UploadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("user", "mdp");

// Copy the contents of the file to the request stream.
using (Stream sourceStream = File.OpenRead(@"C:\xxx\xxx\xxx\xxxxxxxxx.iso"))
using(Stream reqStrm = request.GetRequestStream())
{

    byte[] buffer = new byte[1024 * 1024]; //1 MB buffer
    int count = 0;
    do
    {
        count = sourceStream.Read(buffer, 0, buffer.Length);
        if (count > 0)
        {
            reqStrm.Write(buffer, 0, count);
        }
    }
    while (count > 0);
}           
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
loopedcode
  • 4,863
  • 1
  • 21
  • 21