【问题标题】:Resource interpreted as Image but transferred with MIME type text/html appengine go资源解释为图像,但使用 MIME 类型 text/html appengine 传输
【发布时间】:2014-05-20 19:04:36
【问题描述】:

更新新版本的 go appengine 后,我无法将图像加载到页面中。我不知道我错过了什么,但它以前很简单。 我能够编译代码,但是当我在浏览器上启动应用程序时,我收到了以下消息:

资源解释为图像,但使用 MIME 类型 text/html 传输:http://[...]/img/myimg.jpg

我的应用就是这么简单:

index.html

<!DOCTYPE html>
<html>
<title>Hello</title>
<head>
</head>
<body>
    <h1>Welcome to my website</h1>
    <img src="img/myimg.png" />
</body>
</html>

app.yaml

application: myapp
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

- url: /img
  static_dir: img
  mime_type: image/jpg

你好.go

package hello

import (
    "net/http"
    "text/template"
)

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    template.Must(template.ParseFiles("index.html")).Execute(w, nil)
}

documentation 说:“每个文件都使用与其文件扩展名相对应的 MIME 类型提供服务,除非被目录的 mime_type 设置覆盖”,但无论是否我在 app.yaml 文件中定义了 mime_type。

这个论坛有很多相关问题,但我找不到任何可以有效解决问题的答案。

请注意,我已尝试使用不同的图像(jpg 和 png)来确保图像本身没有问题。我还将相同的应用程序(html 和图像)部署到 apache 网络服务器,它工作正常。

【问题讨论】:

    标签: html google-app-engine go


    【解决方案1】:

    你的 app.yaml 错了,你的 img 处理程序应该是第一个,此时 img/myimg.jpg 将由你的主应用程序处理程序处理,因此是 text/html 响应。

    请记住,处理程序按照定义的顺序进行匹配,如果您使用 /.* 作为您的正则表达式,您的主处理程序将捕获所有内容。

    此外,您的图像标签应该是绝对的,否则如果您的页面深度超过一页,您的相对 img 路径将被附加到页面。

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 2011-07-18
      • 2012-05-20
      • 2014-07-12
      相关资源
      最近更新 更多