|All requests||>||What is the best PHP download file wi...||>||Request new recommendation||>||Featured requests||>||No recommendations|
by Georges Jaroslaw - 8 years ago (2015-01-06)
I want to create a file using AJAX requests to PHP on the server side.
Then I need to download the file to the computer with dialog box to change title location.
The file is data from MySQL to be exported in CSV format.
1. by Manuel Lemos - 8 years ago (2015-01-19) Reply
Downloading files and prompt the user choose the download location on your computer is easy. Just serve the file with the appropriate content-type header set to application/octet-stream which is the generate MIME type for binary data.
However, if you want to request the file via AJAX, that is tricky because there is another approach, the download prompt dialog only appears when serving a file via its own browser request.
Maybe you can create an hidden iframe in the page and make the file be served inside that iframe. That would avoid redirecting the current browser window to another URL. But I don't know a class that does that.
2. by Justin Eldracher - 7 years ago (2015-06-22) in reply to comment 1 by Manuel Lemos Comment
What I do is copy the file I want to download to a specific directory (with PHP of course...) that has a .htaccess file with the following contents: <FilesMatch "\.(?i:csv)$">
ForceType application/octet-stream Header set Content-Disposition attachment
Hope that helps.
3. by Manuel Lemos - 7 years ago (2015-06-22) in reply to comment 2 by Justin Eldracher Comment
That would have the same problem. Either you redirect to the file URL or make it load inside an iframe.