July 30, 201214 yr Hello, I have written a web page that creates a file (by calling a js function that does an ajax call to create a file) and also then creates a button to download the file (again by calling a js function that does an ajax call). the text file is created successfully and the download nearly works - it finds the correct file but echos it to screen instead of forcing a download. I've looked at dozens of examples of how to set up the headers to make the file download but the readfile just directs straight to the screen. The php that's called to download the file is as follows: <?php $database = 'cut'; include '../../../../cut/libraries/ashby/mainconfig.php'; $path="../../../../cut/components/com_quickbooks/output/"; //$path="../output/"; $filename = mysql_real_escape_string( $_GET['filename'] ); header('Pragma: public'); header('Content-Length: ' . filesize($path.$filename)); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$path.$filename); header("Content-Transfer-Encoding: binary"); header('Content-Description: File Transfer'); readfile($path.$filename); ?> I'd really appreciate a fresh pair of eyes Thanks
August 1, 201214 yr Looking at some of my old code, you could try: header('Content-Type: application/force-download'); Might need a more experienced eye if it is some other problem though. Good luck getting it working!
August 1, 201214 yr How are you doing the 'AJAX' download? If you are just getting the response it will always just dump the content of the request. You need to use some kind of ghetto method like create an IFRAME, set the src to the generated file, then the browser will download it. <iframe style="display:none" /> $('iframe').attr('src', 'http://www.parliament.uk/briefing-papers/POST-PN-361.pdf?filename=' + myFilenameVar);
August 18, 201214 yr Looks like the header for Content-Disposition is slightly wrong: try changing from: header('Content-Disposition: attachment; filename='.$path.$filename); to: header('Content-Disposition: attachment; filename="'.$path.$filename.'"');
Create an account or sign in to comment