Force download is slow in xmlhttprequest but not in form handler

Hi All,

I have used XMLHttpRequest to a php script where the script will response with a force download using following headers, example.php

header('Content-Description: File Transfer');
 header("Content-type: application/zip");           
 header("Content-Disposition:  attachment; filename=\"" . basename($newZipFile) . "\"" );
 header("Content-Transfer-Encoding:  binary");  
 header("Pragma:  public");
 header("Cache-Control:  no cache, no-store, must-revalidate"); 
 header("Content-Length:  " . filesize($newZipFile));
 ob_end_flush();
 readfile($newZipFile);
 exit();

and then, in my Javascript, i have made a blob of the response and save it

if(XHR.status == 200)
{
var blob = new Blob([XHR.response], {type: 'application/zip'}); 
var fileName = XHR.getResponseHeader("Content-Disposition").match(/\sfilename="([^"]+)"(\s|$)/)[1];  
saveBlob(blob, fileName);   
}

When force download a 600~MB zip, the ‘Download’ took so long time to start(20 min). BUT, if i replace above the XMLHTTPRequest approach with the Form Handler

<Form action="example.php", method="POST">

Then, the download begin immediately.

Anyone know why such difference happened between this two approach?

My development Enviroment: PHP 5.3.29 Apache 2.2.31

Off-hand, it’s not clear why they would differ. There are some general things to consider if you already haven’t:

  • Does this happen in multiple browsers (IE, Edge, Chrome, Safari)?
  • Have you timed the responses in the developer console (browser-dependent)?
  • Does this always happen with repeated attempts?
  • Does this happen with smaller size files, i.e., start out with a small size file and increase the file size.

You can try searching the API docs at XHR (XMLHttpRequest) to see if there are ways to make the call more efficient as well as Stack Overflow to see if others have encountered similar issues.

Hope this helps… good luck.