更新新版本的 go appengine 后,我无法将图像加载到页面中。我不知道我错过了什么,但它以前非常简单。 我能够编译代码,但是当我在浏览器上启动应用程序时,我收到消息:
Resource interpreted as Image but transferred with MIME type 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>
应用.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 设置覆盖”,但是无论我是否在应用程序中定义 mime_type 都没有区别.yaml 文件。
论坛上有很多相关问题,但我找不到任何能有效解决问题的答案。
请注意,我尝试使用不同的图像(jpg 和 png)以确保这不是图像本身的问题。我还将相同的应用程序(html 和图像)部署到 apache 网络服务器,它工作正常。
最佳答案
你的 app.yaml 是错误的,你的 img 处理程序应该是第一个,此时 img/myimg.jpg 将由你的主应用程序处理程序处理,因此 text/html 响应。
请记住处理程序按照它们定义的顺序进行匹配,如果您使用 /.*
作为您的正则表达式,您的主处理程序将捕获所有内容。
此外,您的图片标签应该是绝对的,否则如果您的页面深度超过一页,您的相对 img 路径将附加到页面。
关于html - 资源解释为图像但使用 MIME 类型 text/html appengine go 传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931033/