1

有很多与此类似的现有问题,但没有一个能回答我的问题。它与动态生成和下载 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 到上面提到的页面。

4

0 回答 0