// I have this code below that does fine for uploading file from a server to another, but I am concerned if the client have a large file to FTP, I am looking for some code to implement the idea of buffering/streaming chunks of the file instead of FTPing the file in one shot, the line of code that I am thinking to change is "requestStream.Write(fileContents, 0, fileContents.Length);" This is where I need to loop through and send the file in chunks to FTP server, any ideas on how to do that?
FtpWebRequest request = null;
StreamReader sourceStream = null;
Stream requestStream = null;
FtpWebResponse response = null;
try
{
// Get the object used to communicate with the server.
request = (FtpWebRequest)WebRequest.Create(@ftpServer + tmpFilename);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.EnableSsl = true;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(ftpUser, connectionStringToFTP);
// Copy the contents of the file to the request stream.
sourceStream = new StreamReader(@fromSource + tmpFilename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
request.ContentLength = fileContents.Length;
requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
if (requestStream != null)
{
requestStream.Flush();
requestStream.Close();
requestStream.Dispose();
}
response = (FtpWebResponse)request.GetResponse();
bool ftpSuccessful = false;
if (response.StatusDescription.StartsWith("226"))
{
ftpSuccessful = true;
}
else
{
//.....etc.
}
}
catch(...)
{
//catch something required to be captured....etc.
}
finally
{
//close/dispose/flush streams here
}