Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET FTP
// 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 12:22pm
Edited 27-Dec-12 12:24pm
v2
Comments
Zoltán Zörgő at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 BillWoodruff 260
3 Shweta N Mishra 244
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
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