Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP/ajax file download

Featured Replies

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

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!

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);

  • 3 weeks later...

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.