如何在Golang中将[][]byte发送到浏览器,并解码为图像?

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

How does one send a [][]byte in Golang to the browser to be decoded as an image

问题

在我的后端Golang Web服务器中,我使用os.ReadDir读取并处理了一个图像目录,这些图像被存储为[][]byte。我希望能够通过GET请求将这些图像发送到浏览器中,并使用JavaScript显示。

我在如何开始发送数据的过程中遇到了困难。我目前正在使用的资源是典型的net/http包和Gorilla Mux/Websockets。

以下是一些示例代码,展示了我当前如何进行GET请求并返回一些JSON。我应该如何发送一个[][]byte数组,而不是渲染模板或JSON呢?

  1. import (
  2. "html/template"
  3. "log"
  4. "net/http"
  5. "encoding/json"
  6. "github.com/gorilla/mux"
  7. )
  8. func ViewSample(rw http.ResponseWriter, req *http.Request) {
  9. type Sample struct {
  10. Id int `json:"id"`
  11. Name string `json:"name"`
  12. User string `json:"user"`
  13. }
  14. params := mux.Vars(req)
  15. sampleId := params["id"]
  16. sample := Sample{
  17. Id: 3,
  18. Name: "test",
  19. User: "testuser",
  20. }
  21. json.NewEncoder(rw).Encode(sample)
  22. }
英文:

In my back-end golang web server I have converted and processed a directory of images that i have read in using os.ReadDir

These images are stored as a [][]byte. I want to be able to send these images through a GET request to be displayed in the browser using Javascript.

I am having trouble figuring out how to begin the process to send the data from the Golang web server. The resources I am currently using are the typical net/http package, and Gorilla Mux/Websockets.

Here is some sample code that shows how I am currently doing a get request which return some json. How can I similarly send a [][]byte array instead of rendering a template or JSON?

  1. import (
  2. "html/template"
  3. "log"
  4. "net/http"
  5. "encoding/json"
  6. "github.com/gorilla/mux"
  7. )
  8. func ViewSample(rw http.ResponseWriter, req *http.Request) {
  9. type Sample struct {
  10. Id int `json:"id"`
  11. Name string `json:"name"`
  12. User string `json:"user
  13. }
  14. params := mux.Vars(req)
  15. sampleId := params["id"]
  16. sample := Sample{
  17. Id: 3,
  18. Name: "test",
  19. User: "testuser"
  20. }
  21. json.NewEncoder(rw).Encode(sample)
  22. }

答案1

得分: 3

如果您的图像存储在[]byte中,您可以直接将其写入http.ResponseWriter

  1. func GetImage(w http.ResponseWriter, r *http.Request) {
  2. image, err := getImage() // getImage示例返回([]byte, error)
  3. if err != nil {
  4. http.Error(w, err.Error(), http.StatusInternalServerError)
  5. return
  6. }
  7. w.Write(image)
  8. }

没有一种原生被客户端理解的方式可以在单个响应中发送多个图像。您可以使用一种方法,在第一次调用时返回一个包含单独获取每个图像链接的JSON文档。

英文:

If your image is stored in a []byte, you can write that directly to the http.ResponseWriter

  1. func GetImage(w http.ResponseWriter, r *http.Request) {
  2. image, err := getImage() // getImage example returns ([]byte, error)
  3. if err != {
  4. http.Error(w, err.Error(), http.StatusInternalServerError)
  5. return
  6. }
  7. w.Write(image)
  8. }

There's no way to send multiple images in a single response which is natively understood by clients. One method you could use is to return a json document on the first call, which contains a list of links to fetch each image individually.

huangapple
  • 本文由 发表于 2015年2月25日 08:18:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/28708919.html
匿名

发表评论

匿名网友

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

确定