How to add Image in HTML file in Go

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

How to add Image in HTML file in Go

问题

首先,我使用Notepad ++创建了一个HTML文件,其中包含以下代码:

  1. <body>
  2. <table>
  3. <tr>
  4. <td>Jill</td>
  5. <td>Smith</td>
  6. <td><img src="test.jpg" border=3 height=100 width=300 /></td>
  7. </tr>
  8. <tr>
  9. <td>Eve</td>
  10. <td>Jackson</td>
  11. <td>94</td>
  12. </tr>
  13. </table>
  14. </body>

以下是用于此目的的Go语言代码:

  1. // * /
  2. func rootHandler(w http.ResponseWriter, r *http.Request) {
  3. if r.URL.Path == "/" {
  4. homeHandler(w, r)
  5. } else {
  6. log.Printf("rootHandler: Could not forward request for %s any further.", r.RequestURI)
  7. errNotFound(w, r)
  8. }
  9. }

我希望在浏览器中加载test.png,但它没有起作用。

英文:

First of all, I created an HTML file using Notepad ++ with this code:

  1. &lt;body&gt;
  2. &lt;table&gt;
  3. &lt;tr&gt;
  4. &lt;td&gt;Jill&lt;/td&gt;
  5. &lt;td&gt;Smith&lt;/td&gt;
  6. &lt;td&gt;&lt;img src=&quot;test.jpg&quot; border=3 height=100 width=300 /&gt;&lt;/td&gt;
  7. &lt;/tr&gt;
  8. &lt;tr&gt;
  9. &lt;td&gt;Eve&lt;/td&gt;
  10. &lt;td&gt;Jackson&lt;/td&gt;
  11. &lt;td&gt;94&lt;/td&gt;
  12. &lt;/tr&gt;
  13. &lt;/table&gt;
  14. &lt;/body&gt;

Below is the Go language Code for this :-

  1. // * /
  2. func rootHandler(w http.ResponseWriter, r *http.Request) {
  3. if r.URL.Path == &quot;/&quot; {
  4. homeHandler(w, r)
  5. } else {
  6. log.Printf(&quot;rootHandler: Could not forward request for %s any further.&quot;, r.RequestURI)
  7. errNotFound(w, r)
  8. }
  9. }

I want test.png should be loaded in browser but its not working.

答案1

得分: 1

你可以为处理位于img目录中的函数添加一个处理程序,例如。

以下是一种可能的实现方式:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func RootHandler(w http.ResponseWriter, r *http.Request) {
  7. fmt.Fprintf(w, "<h1>Hello</h1><img src='/img/MR.png'/>")
  8. }
  9. func main() {
  10. http.HandleFunc("/", RootHandler) // 主页
  11. http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
  12. http.ServeFile(w, r, r.URL.Path[1:])
  13. })
  14. http.ListenAndServe(":8080", nil)
  15. }
英文:

You can add an handler for dealing with function in an img directory, for example.

Here is one possible way to do it:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. )
  6. func RootHandler(w http.ResponseWriter, r *http.Request) {
  7. fmt.Fprintf(w, &quot;&lt;h1&gt;Hello&lt;/h1&gt;&lt;img src=&#39;/img/MR.png&#39;/&gt;&quot;)
  8. }
  9. func main() {
  10. http.HandleFunc(&quot;/&quot;, RootHandler) // homepage
  11. http.HandleFunc(&quot;/img/&quot;, func(w http.ResponseWriter, r *http.Request) {
  12. http.ServeFile(w, r, r.URL.Path[1:])
  13. })
  14. http.ListenAndServe(&quot;:8080&quot;, nil)
  15. }

huangapple
  • 本文由 发表于 2015年12月24日 19:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/34452128.html
匿名

发表评论

匿名网友

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

确定