ComponentPro UltimateFtp

      Append data to an existing file

      Language Filter: AllSend comments on this topic to ComponentPro

      The UploadFile and DownloadFile methods make it possible to upload or download a part of a file. This is often used for appending data to an existing file, or to resume broken transfers. Of course, it is possible to write/read data to/from any other position in a file as well. To upload and download specific file part, use three additional parameters: local offset, remote offset, and the number of bytes you want to transfer. Local offset is not required for stream-based methods - data is read/written from/to the current stream position.

      // Create a new class instance. 
      using (Ftp client = new Ftp())
      {            
          // Connect to the FTP server. 
          client.Connect("localhost");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          string localPath = @"D:\Data\dailyReport.xml";
          string remotePath = "/Data/reports.xml";
          long localFileLength = new FileInfo(localPath).Length;
          long remoteFileLength = client.GetFileLength(remotePath);
      
          // Append header for 'dailyReport.xml' to 'reports.xml' on the server from memory 
          string reportHeader = string.Format("<path>{0}</path>\n", localPath);
          MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(reportHeader));
          client.UploadFile(ms, remotePath,
              remoteFileLength,   // Remote offset 
              ms.Length);         // The number of bytes to upload 
       
          // Update remote length 
          remoteFileLength += ms.Length;
      
          // Append data from 'dailyReport.xml' to 'reports.xml' on the server 
          client.UploadFile(localPath, remotePath,
              0,                 // Local offset 
              remoteFileLength,  // Remote offset 
              localFileLength);  // The number of bytes to upload 
       
          // Disconnect. 
          client.Disconnect();
      }