在Go中使用<img>标签显示本地图片。

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

Use <img> tag in Go to display local image

问题

我该如何使用<img>标签在Go中显示本地图片?

我尝试了以下代码:

fmt.Fprintf(w, "</br><img src='" + path.Join(rootdir,  fileName) + "' ></img>") 

其中rootdir = os.Getwd(),fileName是文件名。

如果我使用相同的路径尝试http.ServeFile,我可以下载图片,但是我想将其嵌入到网页中。

英文:

How can I use the &lt;img&gt; tag to display a local image in Go?

I've tried the following:

fmt.Fprintf(w, &quot;&lt;/br&gt;&lt;img src=&#39;&quot; + path.Join(rootdir,  fileName) + &quot;&#39; &gt;&lt;/img&gt;&quot;) 

where rootdir = os.Getwd() and fileName is the name of the file.

If I try http.ServeFile with the same path then I can download the image, however I would like to embed it in the webpage itself.

答案1

得分: 8

我将在这里陈述一下,我对Go的了解非常糟糕,但我做过一些实验,其中涉及到了这一点,所以也许这至少能指导你朝正确的方向前进。基本上,下面的代码使用一个处理器来处理/images/下的任何内容,该处理器从根目录中的images文件夹中提供文件(在我的情况下是/home/username/go)。然后,你可以在<img>标签中硬编码/images/,或者像之前一样使用path.Join(),将images作为第一个参数。

package main

import (
  "fmt"
  "net/http"
  "os"
  "path"
)


func handler(w http.ResponseWriter, r *http.Request) {
  fileName := "testfile.jpg"
  fmt.Fprintf(w, "<html></br><img src='/images/" + fileName + "' ></html>")
}

func main() {
  rootdir, err := os.Getwd()
  if err != nil {
    rootdir = "No dice"
  }

  // 处理指向 /images/ 的任何内容
  http.Handle("/images/", http.StripPrefix("/images",
        http.FileServer(http.Dir(path.Join(rootdir, "images/"))))))
  http.HandleFunc("/", handler)
  http.ListenAndServe(":8080", nil)
}
英文:

I'll preface this by saying my Go knowledge is atrocious at best, but the few experiments I've done have involved this a bit, so maybe this will at least point you in the right direction. Basically, the below code uses a Handle for anything under /images/ that serves files from the images folder in your root directory (in my case it was /home/username/go). You can then either hardcode /images/ in your &lt;img&gt; tag, or use path.Join() as you did before, making images the first argument.

package main

import (
  &quot;fmt&quot;
  &quot;net/http&quot;
  &quot;os&quot;
  &quot;path&quot;
)


func handler(w http.ResponseWriter, r *http.Request) {
  fileName := &quot;testfile.jpg&quot;
  fmt.Fprintf(w, &quot;&lt;html&gt;&lt;/br&gt;&lt;img src=&#39;/images/&quot; + fileName + &quot;&#39; &gt;&lt;/html&gt;&quot;)
}

func main() {
  rootdir, err := os.Getwd()
  if err != nil {
    rootdir = &quot;No dice&quot;
  }

  // Handler for anything pointing to /images/
  http.Handle(&quot;/images/&quot;, http.StripPrefix(&quot;/images&quot;,
        http.FileServer(http.Dir(path.Join(rootdir, &quot;images/&quot;)))))
  http.HandleFunc(&quot;/&quot;, handler)
  http.ListenAndServe(&quot;:8080&quot;, nil)
}

答案2

得分: 2

也许你可以使用data URI

英文:

Maybe you could use a data URI.

答案3

得分: 0

这对我有用:

package main

import (
   "io"
   "net/http"
   "os"
)

func index(w http.ResponseWriter, r *http.Request) {
   f, e := os.Open(r.URL.Path[1:])
   if e != nil {
      panic(e)
   }
   defer f.Close()
   io.Copy(w, f)
}

func main() {
   http.HandleFunc("/", index)
   new(http.Server).ListenAndServe()
}
英文:

This worked for me:

package main

import (
   &quot;io&quot;
   &quot;net/http&quot;
   &quot;os&quot;
)

func index(w http.ResponseWriter, r *http.Request) {
   f, e := os.Open(r.URL.Path[1:])
   if e != nil {
      panic(e)
   }
   defer f.Close()
   io.Copy(w, f)
}

func main() {
   http.HandleFunc(&quot;/&quot;, index)
   new(http.Server).ListenAndServe()
}

huangapple
  • 本文由 发表于 2012年10月15日 11:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/12888737.html
匿名

发表评论

匿名网友

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

确定