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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记