如何将图像插入HTML Golang文件?

时间:2017-11-04 20:48:00

标签: html go

package main

import (
    "fmt"
    "net/http"
)

func index_handler(w http.ResponseWriter, r *http.Request) {
    // MAIN SECTION HTML CODE
    fmt.Fprintf(w, "<h1>Whoa, Go is neat!</h1>")
    fmt.Fprintf(w, "<title>Go</title>")
    fmt.Fprintf(w, "<img src='gopher.jpeg' alt='gopher' style='width:235px;height:320px;'>")
}

func about_handler(w http.ResponseWriter, r *http.Request) {
    // ABOUT SECTION HTML CODE
    fmt.Fprintf(w, "<title>Go/about/</title>")
    fmt.Fprintf(w, "Expert web design by -")
}

func main() {
    http.HandleFunc("/", index_handler)
    http.HandleFunc("/about/", about_handler)
    http.ListenAndServe(":8000", nil)
}

在我的代码中,我尝试将图像放在屏幕上。所有这一切都是使用alt。我将jpeg文件放在与我的代码相同的目录中。因为我使用Go,有什么必须添加的吗?谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

首先,我建议您将图片,css,js和其他资源放入一个单独的文件夹,而不是你的应用程序所在的文件夹,子文件夹也可以,只需将它们与你的代码分开。

完成后,您需要告诉Go如何以及​​从何处提供这些文件。

以下是一个例子:

package main

import (
    "fmt"
    "net/http"
)

func index_handler(w http.ResponseWriter, r *http.Request) {
    // MAIN SECTION HTML CODE
    fmt.Fprintf(w, "<h1>Whoa, Go is neat!</h1>")
    fmt.Fprintf(w, "<title>Go</title>")
    fmt.Fprintf(w, "<img src='assets/gopher.jpeg' alt='gopher' style='width:235px;height:320px;'>")
}

func about_handler(w http.ResponseWriter, r *http.Request) {
    // ABOUT SECTION HTML CODE
    fmt.Fprintf(w, "<title>Go/about/</title>")
    fmt.Fprintf(w, "Expert web design by JT Skrivanek")
}

func main() {
    http.HandleFunc("/", index_handler)
    http.HandleFunc("/about/", about_handler)
    http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets"))))
    http.ListenAndServe(":8000", nil)
}

这假设你的项目结构如下:

└── app
    ├── assets
    │   └── gopher.jpeg
    └── main.go

并且您已从app文件夹中启动了您的应用。

另请注意,您还必须更改HTML图像链接以反映更改。例如。;而不是<img src='gopher.jpeg' ...您有<img src='assets/gopher.jpeg' ...

答案 1 :(得分:-1)

你只需要使用<img>标签,但你使用'而不是'除了代码看起来很好,但我会考虑使用CSS而不是html来设置图像大小。它不是必需的,但是更常见。

请忽略上述内容,我不正确!

以下代码应该有帮助,它位于here

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

          //w.Write([]byte(fmt.Sprintf("Generating QR code\n")))

          // generate a random string - preferbly 6 or 8 characters
          randomStr := randStr(6, "alphanum")

         // For Google Authenticator purpose
         // for more details see
         // https://github.com/google/google-authenticator/wiki/Key-Uri-Format
         secret = base32.StdEncoding.EncodeToString([]byte(randomStr))
         //w.Write([]byte(fmt.Sprintf("Secret : %s !\n", secret)))

         // authentication link. Remember to replace SocketLoop with yours.
         // for more details see
         // https://github.com/google/google-authenticator/wiki/Key-Uri-Format
         authLink := "otpauth://totp/SocketLoop?secret=" + secret + "&issuer=SocketLoop"

         // Encode authLink to QR codes
         // qr.H = 65% redundant level
         // see https://godoc.org/code.google.com/p/rsc/qr#Level

         code, err := qr.Encode(authLink, qr.H)

         if err != nil {
                 fmt.Println(err)
                 os.Exit(1)
         }

         imgByte := code.PNG()

         // convert byte to image for saving to file
         img, _, _ := image.Decode(bytes.NewReader(imgByte))

         err = imaging.Save(img, "./QRImgGA.png")

         if err != nil {
                 fmt.Println(err)
                 os.Exit(1)
         }

         // in real world application, the QRImgGA.png file should
         // be a temporary file with dynamic name.
         // for this tutorial sake, we keep it as static name.

         w.Write([]byte(fmt.Sprintf("<html><body><h1>QR code for : %s</h1><img src='http://localhost:8080/QRImgGA.png'>", authLink)))
         w.Write([]byte(fmt.Sprintf("<form action='http://localhost:8080/verify' method='post'>Token : <input name='token' id='token'><input type='submit' value='Verify Token'></form></body></html>")))
 }

 func main() {
         http.HandleFunc("/", Home)
         http.HandleFunc("/verify", Verify)

         // this is for displaying the QRImgGA.png from the source directory
         http.Handle("/QRImgGA.png", http.FileServer(http.Dir("./"))) //<---------------- here!

         http.ListenAndServe(":8080", nil)
 }


  [1]: https://www.socketloop.com/tutorials/golang-how-to-display-image-file-or-expose-css-js-files-from-localhost
相关问题