资源被解释为图像,但以 text/html 的 MIME 类型传输 appengine go。

huangapple go评论93阅读模式
英文:

Resource interpreted as Image but transferred with MIME type text/html appengine go

问题

我无法在更新为新版本的go appengine后将图像加载到页面中。我不知道我漏掉了什么,但在此之前,这通常是相当简单的。

我能够编译代码,但当我在浏览器上启动应用程序时,我收到以下消息:

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

我的应用程序就像这样简单:

index.html

<!DOCTYPE html>
<html>
<title>Hello</title>
<head>
</head>
<body>
	<h1>欢迎来到我的网站</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

hello.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)
}

文档中提到:"每个文件都使用与其文件扩展名对应的MIME类型进行服务,除非被目录的mime_type设置覆盖",然而,无论我是否在app.yaml文件中定义mime_type,都没有任何区别。

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

只是作为一种说明,我尝试使用不同的图像(jpg和png)来确保它不是图像本身的问题。我还将相同的应用程序(html和图像)部署到apache web服务器上,它可以正常工作。

英文:

I can't load images into a page after updating for the new version of go appengine. I don't know what I'm missing, but it used to be pretty straight forward before.
I'm able to compile the code, but when I launch the app on the browser I'm getting the message:

> Resource interpreted as Image but transferred with MIME type text/html: http://[...]/img/myimg.jpg

My app is as simple as this:

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

hello.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)
}

The documentation says: "Each file is served using the MIME type that corresponds with its filename extension unless overridden by the directory's mime_type setting", however it does not make a difference whether or not I define the mime_type in the app.yaml file.

There are many related questions on this forum but I couldn't find any answer that could effectively solve the issue.

Just as a note, I've tried with different images (jpg and png) to make sure it wasn't a issue with the image itself. I also deployed the same app (html and image) to apache webserver and it works fine.

答案1

得分: 2

你的app.yaml文件有问题,你的img处理程序应该放在第一位,目前img/myimg.jpg将由你的主要应用程序处理,因此返回的是text/html响应。

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

另外,你的图片标签应该是绝对路径,否则如果你有多个页面深度,你的相对图片路径将被附加到页面上。

英文:

You app.yaml is wrong, your img handler should be first, at the moment img/myimg.jpg will be handled by your main app handler, hence the text/html response.

Remember handlers are matched in the order they are defined and your main handler will catch everything if you use /.* as your regex.

Also your image tag should be absolute otherwise if you have more than one page deep you relative img path will be appended to the page.

huangapple
  • 本文由 发表于 2014年4月8日 16:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/22931033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定