title: Image uploading issue using blueimp jquery file upload plugin in asp.net
body: <p>I have downloaded blueimp jquery file upload plugin example in C# and when running the program I am getting error and unable to upload whatever the filesize maybe.</p>
  
  <p>Any help will be greatly appreciated!
  Thanks in advance.</p>
  
  <p>Here is the result:</p>
  
  <p><img src="https://i.stack.imgur.com/b9xS0.png" alt="enter image description here"></p>
  
  <p>Here is the aspx code:</p>
  
  <pre><code>    &lt;div id="fileupload"&gt;
      &lt;form action="Upload.ashx" method="POST" enctype="multipart/form-data"&gt;
      &lt;div class="fileupload-buttonbar"&gt;
          &lt;label class="fileinput-button"&gt;
              &lt;span&gt;Add files...&lt;/span&gt;
              &lt;input type="file" name="files[]" multiple="multiple" /&gt;
          &lt;/label&gt;
          &lt;%-- &lt;button type="submit" class="start"&gt;Start upload&lt;/button&gt;
          &lt;button type="reset" class="cancel"&gt;Cancel upload&lt;/button&gt; --%&gt;
          &lt;button type="button" class="delete"&gt;
              Delete all files&lt;/button&gt;
          &lt;div class="fileupload-progressbar"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;/form&gt;
      &lt;div class="fileupload-content"&gt;
          &lt;table class="files"&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is the Upload.ashx code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Security.AccessControl;
  using System.Web;
  using System.Web.Script.Serialization;
  
  namespace jQueryUploadTest {
      public class Upload : IHttpHandler {
          public class FilesStatus {
              public string thumbnail_url { get; set; }
              public string name { get; set; }
              public string url { get; set; }
              public int size { get; set; }
              public string type { get; set; }
              public string delete_url { get; set; }
              public string delete_type { get; set; }
              public string error { get; set; }
              public string progress { get; set; }
          }
          private readonly JavaScriptSerializer js = new JavaScriptSerializer();
          private string ingestPath;
          public bool IsReusable { get { return false; } }
          public void ProcessRequest (HttpContext context) {
              var r = context.Response;
              ingestPath = @"C:\temp\ingest\";
  
              r.AddHeader("Pragma", "no-cache");
              r.AddHeader("Cache-Control", "private, no-cache");
  
              HandleMethod(context);
          }
  
          private void HandleMethod (HttpContext context) {
              switch (context.Request.HttpMethod) {
                  case "HEAD":
                  case "GET":
                      ServeFile(context);
                      break;
  
                  case "POST":
                      UploadFile(context);
                      break;
  
                  case "DELETE":
                      DeleteFile(context);
                      break;
  
                  default:
                      context.Response.ClearHeaders();
                      context.Response.StatusCode = 405;
                      break;
              }
          }
  
          private void DeleteFile (HttpContext context) {
              var filePath = ingestPath + context.Request["f"];
              if (File.Exists(filePath)) {
                  File.Delete(filePath);
              }
          }
  
          private void UploadFile (HttpContext context) {
              var statuses = new List&lt;FilesStatus&gt;();
              var headers = context.Request.Headers;
  
              if (string.IsNullOrEmpty(headers["X-File-Name"])) {
                  UploadWholeFile(context, statuses);
              } else {
                  UploadPartialFile(headers["X-File-Name"], context, statuses);
              }
  
  
              WriteJsonIframeSafe(context, statuses);
          }
  
          private void UploadPartialFile (string fileName, HttpContext context, List&lt;FilesStatus&gt; statuses) {
              if (context.Request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
              var inputStream = context.Request.Files[0].InputStream;
              var fullName = ingestPath + Path.GetFileName(fileName);
  
              using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write)) {
                  var buffer = new byte[1024];
  
                  var l = inputStream.Read(buffer, 0, 1024);
                  while (l &gt; 0) {
                      fs.Write(buffer,0,l);
                      l = inputStream.Read(buffer, 0, 1024);
                  }
                  fs.Flush();
                  fs.Close();
              }
  
              statuses.Add(new FilesStatus {
                  thumbnail_url = "Thumbnail.ashx?f=" + fileName,
                  url = "Upload.ashx?f=" + fileName,
                  name = fileName,
                  size = (int)(new FileInfo(fullName)).Length,
                  type = "image/png",
                  delete_url = "Upload.ashx?f=" + fileName,
                  delete_type = "DELETE",
                  progress = "1.0"
              });
  
          }
  
          private void UploadWholeFile(HttpContext context, List&lt;FilesStatus&gt; statuses) {
              for (int i = 0; i &lt; context.Request.Files.Count; i++) {
                  var file = context.Request.Files[i];
                  file.SaveAs(ingestPath + Path.GetFileName(file.FileName));
                  var fname = Path.GetFileName(file.FileName);
                  statuses.Add(new FilesStatus
                               {
                                  thumbnail_url = "Thumbnail.ashx?f=" + fname,
                                  url = "Upload.ashx?f=" + fname,
                                  name = fname,
                                  size = file.ContentLength,
                                  type = "image/png",
                                  delete_url = "Upload.ashx?f=" + fname,
                                  delete_type = "DELETE",
                                  progress = "1.0"
                               });
              }
          }
  
          private void WriteJsonIframeSafe(HttpContext context, List&lt;FilesStatus&gt; statuses) {
              context.Response.AddHeader("Vary", "Accept");
              try {
                  if (context.Request["HTTP_ACCEPT"].Contains("application/json")) {
                      context.Response.ContentType = "application/json";
                  } else {
                      context.Response.ContentType = "text/plain";
                  }
              } catch {
                  context.Response.ContentType = "text/plain";
              }
  
              var jsonObj = js.Serialize(statuses.ToArray());
              context.Response.Write(jsonObj);
          }
  
          private void ServeFile (HttpContext context) {
              if (string.IsNullOrEmpty(context.Request["f"])) ListCurrentFiles(context);
              else DeliverFile(context);
          }
  
          private void DeliverFile (HttpContext context) {
              var filePath = ingestPath + context.Request["f"];
              if (File.Exists(filePath)) {
                  context.Response.ContentType = "application/octet-stream";
                  context.Response.WriteFile(filePath);
                  context.Response.AddHeader("Content-Disposition", "attachment, filename=\"" + context.Request["f"] + "\"");
              } else {
                  context.Response.StatusCode = 404;
              }
          }
  
          private void ListCurrentFiles (HttpContext context) {
              var files = new List&lt;FilesStatus&gt;();
  
              var names = Directory.GetFiles(@"C:\temp\ingest", "*", SearchOption.TopDirectoryOnly);
  
              foreach (var name in names) {
                  var f = new FileInfo(name);
                  files.Add(new FilesStatus
                  {
                      thumbnail_url = "Thumbnail.ashx?f=" + f.Name,
                      url = "Upload.ashx?f=" + f.Name,
                      name = f.Name,
                      size = (int)f.Length,
                      type = "image/png",
                      delete_url = "Upload.ashx?f=" + f.Name,
                      delete_type = "DELETE"
                  });
              }
  
              context.Response.AddHeader("Content-Disposition", "inline, filename=\"files.json\"");
              var jsonObj = js.Serialize(files.ToArray());
              context.Response.Write(jsonObj);
              context.Response.ContentType = "application/json";
          }
      }
  }
  </code></pre>