英文:
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 <img> tag to display a local image in Go?
I've tried the following:
fmt.Fprintf(w, "</br><img src='" + path.Join(rootdir,  fileName) + "' ></img>") 
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 <img> tag, or use path.Join() as you did before, making images the first argument.
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"
  }
  // Handler for anything pointing to /images/
  http.Handle("/images/", http.StripPrefix("/images",
        http.FileServer(http.Dir(path.Join(rootdir, "images/")))))
  http.HandleFunc("/", handler)
  http.ListenAndServe(":8080", 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 (
   "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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论