1

我在 chrome 中遇到了一个奇怪的问题。我从服务器获取图像,它在 Firefox 中运行良好,但在 chrome 中,图像加载一次,然后显示为损坏的图像。

在 Chrome 的控制台中,我收到以下消息:

Resource interpreted as Image but transferred with MIME type text/html: "http://46.137.249.133:8080/Smart/Request/query.htm?ReqType=SessionUnawareAttachmentDownloadReqType&Thumbnail=Yes&AttachmentRowID=344929138455741006"
GET http://46.137.249.133:8080/Smart/Request/query.htm?ReqType=SessionUnawareAttachmentDownloadReqType&Thumbnail=Yes&AttachmentRowID=344929138455741006  

我还检查了 mime 类型,但它的图像/jpeg。这是输出getimagesize()

Array
(
    [0] => 289
    [1] => 202
    [2] => 2
    [3] => width="289" height="202"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
4

2 回答 2

1

在 query.html 中,在输出任何内容之前(理想情况下,在脚本的开头),添加:

header('Content-Type: image/jpeg');

它会告诉您的浏览器返回的内容是 JPEG 图像。

于 2012-08-16T06:11:24.713 回答
1

这是来自的输出curl --verbose <your-url>,您可以看到您的 Web 服务器(在端口 8080 上)将文件宣传为text/html.

< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
[...]
< Content-Disposition: attachment; filename="Hall2.JPG"
< Content-Type: text/html;charset=ISO-8859-1

Content-Type您可以通过在每个 HTTP 响应中添加标题行来设置正确的媒体类型来解决此问题。

在 PHP 中,使用header函数来完成,例如:

header('Content-Type: image/jpeg');

Java Servlet 页面非常相似:

HttpServletResponse res;
res.setContentType("image/jpeg");
于 2012-08-16T06:17:47.890 回答