tags:

views:

25

answers:

1

Hello friends, 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.

A: 

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
yes. It not trigger save dialog. Is any other solution to show save dialog box?
Rahul
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
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