dongliu5475 2014-04-08 08:01
浏览 61
已采纳

资源被解释为图像,但使用MIME类型text / html appengine进行了转移

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条回答 默认 最新

  • dongxi6897 2014-04-08 11:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持