在chrome中下载动态图像会导致“资源被解释为文档......”

时间:2013-10-14 10:00:47

标签: php image google-chrome

有许多与此类似的现有问题,但它们都没有回答我的问题。这与在chrome中动态生成和下载图像有关。使用php站点中的示例;

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

如果我将其放入“export.php”并链接到它,我会按预期获得图像。我希望这是一个下载,所以用户点击链接和图像下载而不是在他们的浏览器中显示。 据称这样做的方法是使用content-disposition标记,就像这样;

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

现在图像不再显示(正如预期的那样)并且所有浏览器都可以在所有浏览器中使用,除了chrome,现在“取消”图像下载完成后下载(根据开发工具) - 还抱怨Resource interpreted as Document but transferred with MIME type image/jpeg在控制台中。

我已经尝试了太阳下的每个响应标题,我做错了什么?

响应标题:

 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

修改

我试图在这里简化描述,但似乎在简化中我可能错过了造成问题的位。该链接启动了一些ajax,它会为上面提到的页面生成一个POST。

0 个答案:

没有答案