title: Redirect response to download file
body: <p>I have a generic handler that I call using this very basic js code: </p>
  
  <pre><code>    var formData = new FormData();
  
      formData.append("fileId", this.model.get("id"));
  
      xhr = new XMLHttpRequest();
  
      xhr.open('POST', '/genericHandlers/DownloadFile.ashx');
  
      xhr.onload = function () {
          if (xhr.status === 200) {
              // Do something here?
          }
      }
  
      xhr.send(formData);
  </code></pre>
  
  <p>My generic handler code seems to look ok to me. Basically I'm attempting to build the response header and I thought that when this handler returned it would begin the download of the file.</p>
  
  <p><strong>Handler code:</strong></p>
  
  <pre><code>   var fileId = context.Request.Form["fileId"];
  
     // File stored in the db as a byte array
  
     var file = (from f in dataContext.OneEVA_Docs_File_Storages
            where Equals(f.ID, fileId)
            select f).FirstOrDefault();
  
     context.Response.Clear();
  
     context.Response.AddHeader("Content-Type", file.ContentType);
     context.Response.AddHeader("Content-Length", file.ContentLength.ToString());
  
     context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}; size={1}", file.Name, file.ContentLength));
  
     context.Response.BinaryWrite(file.File_Image.ToArray());
     context.Response.Flush();
  
     context.Response.End();
  </code></pre>
  
  <p>The <code>XHR</code> request completes ok. Here is the response header I get back:</p>
  
  <pre><code>Cache-Control:private
  Connection:Close
  Content-Disposition:attachment; filename=Mikes File; size=1860113
  Content-Length:1860113
  Content-Type:image/jpeg
  Date:Wed, 16 May 2012 14:08:01 GMT
  Server:ASP.NET Development Server/10.0.0.0
  X-AspNet-Version:4.0.30319
  </code></pre>
  
  <p>What am I missing? What am I doing wrong?</p>