I’m using the example here:
http://msdn.microsoft.com/en-us/library/ms229715.aspx
to upload a large 1GB file to an FTP server. However it chokes on the line:
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
with an out-of-memory exception. Is there a way to stream the file from disk directly to the FTP server without reading all of it to memory at once? I expect that I can chunk it and send it bit by bit but I would have thought that I would be able to point the read stream at the write stream…
You can try simply writing to the request stream in chunks – if you’re using .NET 4,
Stream.CopyTowill make this really easy:You might want to look at
FtpWebRequest‘s documentation to see if there’s anything about buffering – basically you want to disable any buffering that’s there.(On a side note, it’s a shockingly poor example in MSDN, in terms of resource handling without
usingstatements. Ick.)