1

I have a script to output a mysql result to excel, which is working just fine in all browsers. However, I need the script to also work on iPad, opening the excel in Safari. At this moment, the script just "hangs" on the iPad, it keeps loading. Is this a headers problem? The code headers at this moment:

header( 'Content-Transfer-Encoding: binary' );
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/vnd.ms-excel');
//header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;filename="' . $_POST['Client_FirstName'] . ' ' .     $_POST['Client_LastName'] . ' ' . $_POST['month'] . '-' . $_POST['year'] . '.xlsx"');
header('Cache-Control: max-age=0');
header("Pragma: public");
header("Expires: 0");
Borniet
  • 3,544
  • 4
  • 24
  • 33
  • As far as I know Safari on IOS can not handle file downloads. – Michi Jul 12 '12 at 10:38
  • And opening it in a new window? – Borniet Jul 12 '12 at 10:38
  • Neither, Right now I don't think there's a solution without using a native app. – Michi Jul 12 '12 at 10:41
  • Maybe that could help you http://stackoverflow.com/questions/2294286/opening-native-app-from-safari – Michi Jul 12 '12 at 11:04
  • Thanks Michi, but that only works if you get to install an app on the iPhone, right? What I am looking for is like when, on my iPad, I open Dropbox in Safari, I get the interface, in Safari, and when I click on an excel file, it opens in a new tab in Safari, also giving me the option to "open with" other apps (dropbox,...) – Borniet Jul 12 '12 at 12:55
  • The idea behind the link I posted is to have special links to open the Excel file in an native app... there are several ones that can handle XLS. Maybe you could provide more links and let the user choose depending on the apps he has installed. – Michi Jul 12 '12 at 14:26
  • I don't know if that could work.. but maybe you could open the Excel file using Google Docs... haven't tried that one though. – Michi Jul 12 '12 at 14:27
  • Apparently it was the application/vnd.ms-excel header that caused the problem. Setting it to octet-stream solved the issue, but then caused problems in IE8. So I used this code to totally solve the problem:`if(stristr($_SERVER['HTTP_USER_AGENT'], 'ipad') OR stristr($_SERVER['HTTP_USER_AGENT'], 'iphone') OR stristr($_SERVER['HTTP_USER_AGENT'], 'ipod')) { header("Content-Type: application/octet-stream"); }else{ header('Content-Type: application/vnd.ms-excel'); }` – Borniet Jul 13 '12 at 09:26
  • You mean IOS Safari does open the Excel file? – Michi Jul 13 '12 at 09:35
  • Yes it does now. Great isn't it :-) – Borniet Jul 13 '12 at 09:42
  • It's great and something I never expected. You could answer your own question so others can find the solution. – Michi Jul 13 '12 at 09:44
  • Thanks Michi, I thought I had already answered it by posting it to the comments :-) I'm new here, so still learning :-) Added it as an answer now! – Borniet Jul 13 '12 at 10:02

1 Answers1

2

Apparently it was the application/vnd.ms-excel header that caused the problem. Setting it to octet-stream solved the issue, but then caused problems in IE8. So I used this code to totally solve the problem:

if(stristr($_SERVER['HTTP_USER_AGENT'], 'ipad') OR stristr($_SERVER['HTTP_USER_AGENT'],     'iphone') OR stristr($_SERVER['HTTP_USER_AGENT'], 'ipod')) 
{ 
  header("Content-Type: application/octet-stream"); 
}else{ 
  header('Content-Type: application/vnd.ms-excel'); 
}
Borniet
  • 3,544
  • 4
  • 24
  • 33