title: Pushing Headers and AJAX
body: <p>I have a script that forces a download and I make a call to this via Javascript. However, the dialog box doesn't pop up, here is the download.php script:</p>
  
  <pre><code>header("Pragma: public"); // required
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: private",false); // required for certain browsers 
  header("Content-Type: $ctype");
  // change, added quotes to allow spaces in filenames, by Rajkumar Singh
  header("Content-Disposition: attachment; filename=\"".basename($properFilename)."\";" );
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".filesize($filename));
  readfile("$filename");
  exit();
  </code></pre>
  
  <p>Here is the Javascript (using JQuery):</p>
  
  <pre><code>///force download
             $.ajax({
             type: "GET",
             url: "download.php",
             data: 'file=' + msg + '&amp;properFilename=' + properFileName,
             success: function(msg){
  
                window.location.href = msg;
  
           });//ajax
  </code></pre>
  
  <p>This redirects my browser to another page rather than showing the down dialog box.</p>
  
  <p>I know the JS variable msg contains the file with the right headers but I don't know what to do with it to get it to display the download dialog box.</p>
  
  <p>Thanks all</p>
  
  <p>p.s. Didn't know where to put this thread JS or PHP.</p>
  
  <h2>EDIT:</h2>
  
  <p>I have the right approach I am sure of that :) - A user comes to my site, they fill in a form and they press submit. After a few seconds their fle should show up in a dialog box that they can download. To do this:</p>
  
  <p>I make an AJAX call to get the file and download it. I use the PHP script to send the headers. Now all I need is a way to get the dowload dialog box to show up!!</p>