Click here to Skip to main content
12,945,945 members (52,720 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
// 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
}
Posted 27-Dec-12 11:22am
Updated 27-Dec-12 11:24am
v2
Comments
Zoltán Zörgő 31-Dec-12 3:23am
   
Any progress?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Take a look at this:
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.html[^]

*** EDIT ***
There is also a article written here with a class you can use Simple C# FTP Class[^]
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 18:37pm
   
Should be helpful, a 5.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

FTP does not support chunk parallel upload only parallel chunk download.
But FTP extension RFC 3659[^] introduced the REST command to resume broken transfer. As I know FtpWebRequest is supporting this extension, ContentOffset[^] is there, but be aware, that the server also has to support it!
You can also implement the whole protocol, and here you have a good article: http://www.dreamincode.net/forums/topic/35902-create-an-ftp-class-library-in-c%23/[^]
  Permalink  
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 18:37pm
   
A good one, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,205
Maciej Los 2,453
Jochen Arndt 1,935
ppolymorphe 1,795


Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100