FAQ
package main

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

func main() {
     http.HandleFunc("/", test)
     http.ListenAndServe(":8083", nil)
}

func test(w http.ResponseWriter, r *http.Request) {
     index := template.Must(template.ParseFiles("index.html"))

     index.Execute(w, nil)
}


index.html
<!DOCTYPE html>
<html>
     <head>
         <title>Image</title>
     </head>
     <body>
          <img src="test.png" >
     </body>
<html>


Resource interpreted as Image but transferred with MIME type text/html:
"http://localhost:8083/test.png".

why can not I show the picture inside the img tag and why MIME type not
image/png and how can i fixed that

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • David Symonds at May 8, 2013 at 12:14 am
    Try viewing http://localhost:8083/test.png in your browser.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at May 8, 2013 at 1:19 am

    On Wednesday, May 8, 2013 2:14:02 AM UTC+2, David Symonds wrote:
    Try viewing http://localhost:8083/test.png in your browser.
    same thing I can't see image
    and MIME type text/html
    Resource interpreted as Image but transferred with MIME type text/html: "
    http://localhost:8083/test.png <http://localhost:8083/rmbunctious.png>".

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • David Symonds at May 8, 2013 at 1:22 am
    So why do you think your server is sending your HTML for the /test.png
    request?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at May 8, 2013 at 1:35 am

    On Wednesday, May 8, 2013 3:22:01 AM UTC+2, David Symonds wrote:
    So why do you think your server is sending your HTML for the /test.png
    request?


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at May 8, 2013 at 1:36 am

    On Wednesday, May 8, 2013 3:22:01 AM UTC+2, David Symonds wrote:
    So why do you think your server is sending your HTML for the /test.png
    request?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at May 8, 2013 at 1:39 am

    On Wednesday, May 8, 2013 3:22:01 AM UTC+2, David Symonds wrote:
    So why do you think your server is sending your HTML for the /test.png
    request?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Buzzlight at May 8, 2013 at 1:54 am
    xxx.png is handled by function test also, so it's text/html, should use
    ServerFile to handle static files request.

    在 2013年5月8日星期三UTC+8上午2时12分42秒,Tester写道:
    package main

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

    func main() {
    http.HandleFunc("/", test)
    http.ListenAndServe(":8083", nil)
    }

    func test(w http.ResponseWriter, r *http.Request) {
    index := template.Must(template.ParseFiles("index.html"))

    index.Execute(w, nil)
    }


    index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Image</title>
    </head>
    <body>
    <img src="test.png" >
    </body>
    <html>


    Resource interpreted as Image but transferred with MIME type text/html: "
    http://localhost:8083/test.png".

    why can not I show the picture inside the img tag and why MIME type not
    image/png and how can i fixed that
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at May 8, 2013 at 2:23 am
    func test(w http.ResponseWriter, r *http.Request) {

         index := template.Must(template.ParseFiles("index.html"))

         http.ServeFile(w, r, "index.html")

         index.Execute(w, nil)
    }

    same thing not work :/

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at May 8, 2013 at 2:38 am

    On Wed, May 8, 2013 at 12:23 PM, Tester wrote:

    func test(w http.ResponseWriter, r *http.Request) {

    index := template.Must(template.ParseFiles("index.html"))

    http.ServeFile(w, r, "index.html")

    index.Execute(w, nil)
    }

    same thing not work :/

    You don't seem to understand the way html and http work.
    The " <img src="test.png" >" in your template instructs the browser to
    fetch a file called 'test.png' relative to the current url directory.
    In your case this the file "/test.png".

    Your http server has a handler called 'test' that handles any requests
    starting with '/'. Thus the request that the browser makes for '/test.png'
    is handled by this handler.
    This handler is returning html and not an image. If you want to serve an
    image then you need a handler that will serve an image.
    eg.

    http.HandleFunc("/test.png", serveimage)
    func serveimage(w http.ResponseWriter, r *http.Request) {
         http.ServeFile(w, r, "test.png")
    }

    Will add a handler that will serve the file 'test.png' from the program's
    working directory to any requests for urls that start with "/test.png".

    If you wish to serve a lot of different files from a directory tree then
    you'll want to read about the http.FileServer handler.
    http://golang.org/pkg/net/http/#FileServer

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at May 8, 2013 at 3:32 am
    Thank you very much for your understanding me the way html and http work
    On Wednesday, May 8, 2013 4:38:13 AM UTC+2, Jesse McNelis wrote:

    On Wed, May 8, 2013 at 12:23 PM, Tester <ma....@gmail.com <javascript:>>wrote:
    func test(w http.ResponseWriter, r *http.Request) {

    index := template.Must(template.ParseFiles("index.html"))

    http.ServeFile(w, r, "index.html")

    index.Execute(w, nil)
    }

    same thing not work :/

    You don't seem to understand the way html and http work.
    The " <img src="test.png" >" in your template instructs the browser to
    fetch a file called 'test.png' relative to the current url directory.
    In your case this the file "/test.png".

    Your http server has a handler called 'test' that handles any requests
    starting with '/'. Thus the request that the browser makes for '/test.png'
    is handled by this handler.
    This handler is returning html and not an image. If you want to serve an
    image then you need a handler that will serve an image.
    eg.

    http.HandleFunc("/test.png", serveimage)
    func serveimage(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "test.png")
    }

    Will add a handler that will serve the file 'test.png' from the program's
    working directory to any requests for urls that start with "/test.png".

    If you wish to serve a lot of different files from a directory tree then
    you'll want to read about the http.FileServer handler.
    http://golang.org/pkg/net/http/#FileServer



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 7, '13 at 10:53p
activeMay 8, '13 at 3:32a
posts11
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase