无法加载 Chrome 中图像的资源


Failed to load resource for Images in 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  

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

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

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

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

在 PHP 中,使用header函数执行此操作,例如:

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

这与Java Servlet页面非常相似:

HttpServletResponse res;
res.setContentType("image/jpeg");

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

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

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