1

There are a bunch of existing questions similar to this one, none of them answer my problem though. It's to do with dynamically generating and downloading images in chrome. Using the example from the php site;

http://php.net/manual/en/function.imagejpeg.php

<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);
?>

If I put this into "export.php" and link to it, I get the image as expected. I want this to be a download though, so that the user clicks the link and the image downloads rather than appears in their browser. Allegedly the way to do this is with the content-disposition tag, like so;

<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

//This is literally the only line I have added
header('Content-Disposition: attachment; filename="foo.jpg"');    

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);
?>

And now the image no longer displays (as expected) and all works in all browsers EXCEPT chrome, which now "cancels" the image download when it's done downloading (according to dev tools) - also complains that Resource interpreted as Document but transferred with MIME type image/jpeg in the console.

I have tried every response header under the sun, what am I doing wrong?

response headers:

 Cache-Control:max-age=0
 Connection:Keep-Alive
 Content-Disposition:attachment; filename="foo.jpg"
 Content-Length:1302
 Content-Type:image/jpeg
 Date:Mon, 14 Oct 2013 09:55:32 GMT
 Expires:Mon, 14 Oct 2013 09:55:32 GMT
 Keep-Alive:timeout=15, max=43
 Server:Apache/2.2.16 (Debian)
 X-Powered-By:PHP/5.3.3-7+squeeze15

EDIT

I was trying to simplify the description here but it seems like in simplifying I may have missed out the bit that is causing the issue. The link kicks off some ajax, which generates a POST to the page mentioned above.

Mikey Hogarth
  • 4,672
  • 7
  • 28
  • 44
  • possible duplicate of [Resource interpreted as Document but transferred with MIME type image/jpeg](http://stackoverflow.com/questions/13985003/resource-interpreted-as-document-but-transferred-with-mime-type-image-jpeg) – Kami Oct 14 '13 at 10:17
  • Are you sure the file is not downloaded? – t.niese Oct 14 '13 at 10:21
  • Absolutely positive. It isn't appearing in the downloads window. Been banging my head against the wall about this for around 2 hours! – Mikey Hogarth Oct 14 '13 at 10:27
  • This [Issue](https://code.google.com/p/chromium/issues/detail?id=163687) describes your _problem_, but there they mention it should be downloaded: (`[...] The canceled request is an artifact of how Chrome and WebKit interact around downloads (the browser process tells WebKit the request was cancelled because it's going to handle it directly rather than handing the data off to WebKit) [...]`) – t.niese Oct 14 '13 at 10:28
  • have a look at this, maybe it'll help: http://stackoverflow.com/questions/3476362/how-to-force-a-file-to-download-in-php – Zathrus Writer Oct 14 '13 at 10:31
  • I checked your source (did a plain copy and past of your source) and other scripts with `Content-Disposition` all of them show the message and the `cancel` event. But download still works with Chrome (also with your script). I would guess that it's something about your chrome e.g. about it's cache. – t.niese Oct 14 '13 at 10:45
  • Thanks for all the replies - it looks like this might have something to do with the way the file is being requested rather than the response, I'll update the description. – Mikey Hogarth Oct 14 '13 at 11:33
  • @MikeyHogarth How do you create the `POST` ? With `js` (`XMLHttpRequest`) or with a form element? – t.niese Oct 14 '13 at 11:45
  • It's actually the "amcharts" plugin that does it all - it's an old version, I'm looking through the source now to see if I can find how they're doing it (from my perspective, I'm just following their guide and interrogating "$data = &$_POST;"). – Mikey Hogarth Oct 14 '13 at 12:03

0 Answers0