1

I am trying to download file through ajax request. I have below code in my php file to dowload

$filedata="File data here"
header("Content-type: application/octet-stream");
header("Content-Disposition:attachment; filename=report.txt");
header('Content-Transfer-Encoding: binary');
header("Pragma: public");
header("Expires: 0");
echo $filedata;

It giving me ajax response but not providing file download dialog box. Is any solution? Thanks in advance.

Awan
  • 18,096
  • 36
  • 89
  • 131
Rahul
  • 2,511
  • 3
  • 20
  • 20

1 Answers1

1

I think ajax is blocking the request.

Since its ajax that reads from the server it might not trigger the file save dialog.

David Mårtensson
  • 7,550
  • 4
  • 31
  • 47
  • yes. It not trigger save dialog. Is any other solution to show save dialog box? – Rahul Oct 29 '10 at 14:34
  • Don't use an AJAX request. Just do a `document.location` pointing at the url directly (for GET requests) or build a hidden form and submit that for POST requests. – Marc B Oct 29 '10 at 15:37
  • 1
    Or for file down load, just add a link. If the browser get a content.Attachement it will not change page usually but only download the file and save it, staying on the current page. ANd if you want to be certain, add target="_BLANK" to the a tag and it will download in a new browser window. – David Mårtensson Oct 29 '10 at 19:12