title: using Plupload with ASP.NET/C#
body: <h2>UPDATE</h2>
  
  <p>I was able to get everything to work properly and I just wanted to post back with the updated code. I used Darin Dimitrov's suggestion on using a separate generic http handler for handling the file uploads and so this is the code I came up with for that... let me know if you have questions.</p>
  
  <pre><code>&lt;%@ WebHandler Language="C#" Class="Upload" %&gt;
  
  using System;
  using System.Configuration;
  using System.Data;
  using System.Data.SqlClient;
  using System.IO;
  using System.Net;
  using System.Web;
  
  public class Upload : IHttpHandler {
  
      public void ProcessRequest(HttpContext context) {
  
          /**
           * If 'newTicket' is "false", then the directory to upload to already exists and we can extract it from
           * the 'ticketID' POST parameter.
           * 
           * If 'newTicket' is "true", then this is a new Ticket submission so we need to work with a NEW directory 
           * on the server, so the ID needs to be 1 more than the total number of directories in ~/TicketUploads/
           */
          String newTicket = context.Request["newTicket"] != null ? context.Request["newTicket"] : String.Empty;
          int theID = -1;
          if (newTicket.Equals("true")) {
  
              // we need to calculate a new ID
              theID = getNewID(context); // calculate the new ID = # of rows
              theID++; // add 1 to make it unique
          } else if (newTicket.Equals("false")) {
  
              // we can just get the ID from the POST parameter
              theID = context.Request["ticketID"] != null ? Convert.ToInt32(context.Request["ticketID"]) : -1;
          } else {
  
              // something went wrong with the 'newTicket' POST parameter
              context.Response.ContentType = "text/plain";
              context.Response.Write("Error with 'newTicket' POST parameter.");
          }
  
          // if theID is negative, something went wrong... can't continue
          if (theID &lt; 0) {
              return;
          }
  
          // ready to read the files being uploaded and upload them to the correct directory
          int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0;
          string fileName = context.Request["name"] != null ? context.Request["name"] : string.Empty;
          var uploadPath = context.Server.MapPath("~/TicketUploads/" + theID + "/");
          HttpPostedFile fileUpload = context.Request.Files[0];
  
          // if the NEW directory doesn't exist, create it
          DirectoryInfo di = new DirectoryInfo("" + uploadPath + "");
          if (!(di.Exists)) {
              di.Create();
          }
  
          using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append)) {
              var buffer = new byte[fileUpload.InputStream.Length];
              fileUpload.InputStream.Read(buffer, 0, buffer.Length);
              fs.Write(buffer, 0, buffer.Length);
          }
  
          context.Response.ContentType = "text/plain";
          context.Response.Write("File uploaded.");
          return;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I'm trying to integrate the <a href="http://www.plupload.com/" rel="noreferrer">Plupload</a> file uploader in ASP.NET using C#. I've read the <a href="http://www.angrymonkeys.com.au/blog/2010/02/26/using-plupload-with-asp-net/" rel="noreferrer">Angry Monkeys article</a> as well as the <a href="http://blog.marcovalsecchi.it/2010/10/video-upload-plupload-with-aspnet-35.html" rel="noreferrer">Marco Valsecchi blog post</a> but I'm a little lost.</p>
  
  <p>The C# that the above articles suggest is roughly similar to the following:</p>
  
  <pre><code>int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
  string fileName = Request.QueryString["name"] != null ? Request.QueryString["name"] : string.Empty;
  
  HttpPostedFile fileUpload = Request.Files[0];
  
  using (FileStream fs = new FileStream(Server.MapPath("~/TicketUploads/" + fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
  {
      Byte[] buffer = new Byte[fileUpload.InputStream.Length];
      fileUpload.InputStream.Read(buffer, 0, buffer.Length);
  
      fs.Write(buffer, 0, buffer.Length);
      fs.Close();
  }
  </code></pre>
  
  <p>First, I have set up the Plupload configuration as follows:</p>
  
  <pre><code>$("#plupload_container").pluploadQueue({
    runtimes: 'html5,gears,flash,silverlight,html4',
    flash_swf_url: '../plupload/js/plupload.flash.swf',
    silverlight_xap_url: '../plupload/js/plupload.silverlight.xap',
    filters: [
      { title: "Image files", extensions: "jpg,gif" },
      { title: "Zip files", extensions: "zip" },
      { title: "Document files", extensions: "doc,pdf,txt" }
    ]
  });
  </code></pre>
  
  <p>... but I feel like I'm missing something here that will be necessary for the uploading to work.</p>
  
  <p>I guess my main question is how do I call the above C# code so that the uploading can begin? I have a form on a page named <code>SubmitRequest.aspx</code>. Clicking 'Submit' on the form results in the following:</p>
  
  <pre><code>$('form').submit(function (e) {
  
    // Validate number of uploaded files
    if (uploader.total.uploaded == 0) {
      // Files in queue upload them first
      if (uploader.files.length &gt; 0) {
        // When all files are uploaded submit form
        uploader.bind('UploadProgress', function () {
          if (uploader.total.uploaded == uploader.files.length)
            $('form').submit();
        });
        uploader.start();
      }
      e.preventDefault();
    }
  });
  </code></pre>
  
  <p>... so the uploader starts when 'Submit' is clicked and uploads the files. Once that is done, the rest of the form is submitted. I don't understand how to link this event to the C# code that will handle the uploading to a folder <code>TicketUploads</code> on the server.</p>
  
  <p>I apologize for the longish post, but I would appreciate any help :)</p>