title: Plupload, C#, FTP and Chunking
body: <p>I'm using Plupload to manage large file uploads.  I have written a C# handler to upload using FTPWebRequest and it appears to work ok.  The trouble is I would like to use chunking (<a href="http://www.plupload.com/docs/Chunking" rel="nofollow">http://www.plupload.com/docs/Chunking</a>).  When i enable chunking, I get multiple files uploaded to my FTP server called blobxxx, blobyyyy.  The question is, how do I join them all together at the end?  Here is my code - thanks for any help:</p>
  
  <p>Plupload.aspx</p>
  
  <pre><code>    &lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="plupload.aspx.cs" Inherits="plupload" %&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"/&gt;
  
  &lt;title&gt;Plupload - Custom example&lt;/title&gt;
  
  &lt;!-- production --&gt;
  
      &lt;link href="plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css" rel="stylesheet" type="text/css" /&gt;
  
    &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" charset="UTF-8"&gt;&lt;/script&gt;
  
      &lt;script type="text/javascript" src="plupload/js/plupload.full.min.js"&gt;&lt;/script&gt;
  
      &lt;script src="plupload/js/jquery.plupload.queue/jquery.plupload.queue.min.js" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;!-- debug 
  &lt;script type="text/javascript" src="../js/moxie.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="../js/plupload.dev.js"&gt;&lt;/script&gt;
  --&gt;
  
  &lt;/head&gt;
  &lt;body style="font: 13px Verdana; background: #eee; color: #333"&gt;
  
  &lt;h1&gt;Custom example&lt;/h1&gt;
  
  &lt;p&gt;Shows you how to use the core plupload API.&lt;/p&gt;
  
  &lt;div id="filelist"&gt;Your browser doesn't have Flash, Silverlight or HTML5 support.&lt;/div&gt;
  &lt;br /&gt;
  
  &lt;div id="uploader"&gt;
      &lt;p&gt;Your browser doesn't have Flash, Silverlight or HTML5 support.&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  // Initialize the widget when the DOM is ready
  $(function() {
      // Setup html5 version
      $("#uploader").pluploadQueue({
          // General settings
          runtimes : 'html5,flash,silverlight,html4',
          url : "FileUpload.ashx",
          chunk_size: '1mb',
          max_retries: 3,
  
          rename : true,
          dragdrop: true,
  
  
          // Resize images on clientside if we can
          resize: {
              width : 200,
              height : 200,
              quality : 90,
              crop: true // crop to exact dimensions
          },
  
  
          // Flash settings
          flash_swf_url : 'plupload/js/Moxie.swf',
  
          // Silverlight settings
          silverlight_xap_url : 'plupload/js/Moxie.xap'
      });
  
      $("#uploader").bind("Error", function (upload, error) {
          alert(error.message);
      });
  
      // only allow 5 files to be uploaded at once
      $("#uploader").bind("FilesAdded", function (up, filesToBeAdded) {
          if (up.files.length &gt; 5) {
              up.files.splice(4, up.files.length - 5);
              showStatus("Only 5 files max are allowed per upload. Extra files removed.", 3000, true);
              return false;
          }
          return true;
      });
  
  });
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>FileUpload.ashx</p>
  
  <pre><code>&lt;%@ WebHandler Language="C#" Class="FileUpload" %&gt;
  
  using System;
  using System.Web;
  using System.Net;
  using System.IO;
  using System.Text;
  
  public class FileUpload : IHttpHandler {
  
  
      public void ProcessRequest(HttpContext context)
      {
          if (context.Request.Files.Count &gt; 0)
          {
  
              for (int a = 0; a &lt;= context.Request.Files.Count - 1; a++) 
              {
                  FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + context.Request.Files[a].FileName));
  
                  clsRequest.Credentials = new NetworkCredential("xxx", "yyy");
                  clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
  
  
                  Stream streamReader = context.Request.Files[a].InputStream;                
                  byte[] bFile = new byte[streamReader.Length];
  
                  streamReader.Read(bFile, 0, (int)streamReader.Length);
                  streamReader.Close();
                  streamReader.Dispose();
  
                  Stream clsStream = clsRequest.GetRequestStream();
                  //clsStream.Write(bFile, 0, bFile.Length);
  
                  // Write the local stream to the FTP stream
                  // 2 bytes at a time
                  int offset = 0;
                  int chunk = (bFile.Length &gt; 2048) ? 2048 : bFile.Length;
                  while (offset &lt; bFile.Length)
                  {
                      clsStream.Write(bFile, offset, chunk);
                      offset += chunk;
                      chunk = (bFile.Length - offset &lt; chunk) ? (bFile.Length - offset) : chunk;
                  }
  
                  clsStream.Close();
                  clsStream.Dispose();
  
                  FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();
  
                  response.Close();
  
              }
          }
      }
  
  
  }
  </code></pre>
  
  <p>UPDATE**</p>
  
  <p>I've changed:</p>
  
  <pre><code>FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + context.Request.Files[a].FileName));
  </code></pre>
  
  <p>to:</p>
  
  <pre><code> FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + "staticFileName.zip"));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>clsRequest.Method = System.Net.WebRequestMethods.Ftp.AppendFile;
  </code></pre>
  
  <p>It appears to upload ok.  Need to just test the chunking...</p>