Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
FtpWebRequest request = null;
StreamReader sourceStream = null;
Stream requestStream = null;
FtpWebResponse response = null;


try
          {
C#

// Get the object used to communicate with the server.
C#
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.
C#
request.Credentials = new NetworkCredential(ftpUser, connectionStringToFTP);
// Copy the contents of the file to the request stream.
C#
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.
C#
}
          }
catch(...)
{
//catch something required to be captured....etc.
C#
}
finally
{
//close/dispose/flush streams here
C#
}
Posted
Updated 27-Dec-12 11:24am
v2
Comments
Zoltán Zörgő 31-Dec-12 3:23am    
Any progress?

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[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 18:37pm    
Should be helpful, a 5.
—SA
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/[^]
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900