title: Why are files are different when downloading from an ASP.NET (AJAX download with Blob)
body: <p>Using MVC 4.0, I have used the following code to create a download files from the server from an ajax source (using the latest firefox):</p>
  
  <p>This works fine if the output involves are textual files such as csv or txt files, however, when it comes to files like zip or xlsx, it seems the downloaded file is different from the original source (i.e. the zip generated within the server are 15K, but the one downloaded are 26K)</p>
  
  <p>I have been struggling for a few days, can I ask if anyone should shred some light on why it will works for csv/text files, but not for zip or xlsx files?</p>
  
  <p>Many thanks</p>
  
  <p>Controller:</p>
  
  <pre><code>Public Function download(dataIn As myObject) As ActionResult
          'some processing
          'generated zip files and return with the full path            
          Dim zipFullPath = generateFiles(dataIn)
  
          Response.Clear()
          Response.ContentType = "application/zip"           
          Response.AddHeader("Content-Disposition", "attachment; filename=Out.zip")
          Dim fileLength = New IO.FileInfo(zipFullPath).Length
          'fileLength  reads about 15K of data
          Response.AddHeader("Content-Length", fileLength)
          Response.TransmitFile(zipFullPath)
          Response.End()
          Return View()
      End Function
  </code></pre>
  
  <p>JavaScript:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "reports/download",
      data: jData,
      contentType: "application/json; charset=utf-8",
      success: function(response, status, xhr) {
          // check for a filename
          var filename = "";
          var disposition = xhr.getResponseHeader('Content-Disposition');
          if (disposition &amp;&amp; disposition.indexOf('attachment') !== -1) {
              var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
              var matches = filenameRegex.exec(disposition);
              if (matches != null &amp;&amp; matches[1]) filename = matches[1].replace(/['"]/g, '');
          }
  
          var type = xhr.getResponseHeader('Content-Type');
          var blob = new Blob([response], { type: type });
  
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
              // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
              window.navigator.msSaveBlob(blob, filename);
          } else {
              var URL = window.URL || window.webkitURL;
              var downloadUrl = URL.createObjectURL(blob);
  
              if (filename) {
                  // use HTML5 a[download] attribute to specify filename
                  var a = document.createElement("a");
                  // safari doesn't support this yet
                  if (typeof a.download === 'undefined') {                
                      window.location = downloadUrl;
                  } else {
                      a.href = downloadUrl;
                      a.download = filename;
                      document.body.appendChild(a);
                      a.click();
  
                      //Here is the problem, the original is about 15k,  
                      // but the download file is about 26K
  
                  }
              } else {                    
                  window.location = downloadUrl;
              }
  
              setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
          }
      },
      error: function (data) {
          alert('Error');
      }
  });
  </code></pre>