R
R
righty_dev2021-01-09 15:19:37
MySQL
righty_dev, 2021-01-09 15:19:37

How to save and retrieve images from db using go?

The task is to save several pictures from one form field in the database and display them in html. I tried to save to the database in blob format, but now I can't decrypt and display these images. Several images from one input fall into one field of the database table. I was looking for answers to the question, it was said that you can also save images in a folder, and write the path to the file in the database. I would be glad to help with any of the methods, most importantly with an example of the go code (I did not find anything on the network other than php).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Lapshin, 2021-02-05
@flame0399

Here's what I did to save .xlsx files. It seems to work for pictures as well.

func DownloadFile(filepath, url string) error {
  resp, err := http.Get(url)
  if err != nil {
    return err
  }
  defer resp.Body.Close()
  out, err := os.Create(filepath)
  if err != nil {
    return err
  }

  defer out.Close()

  _, err = io.Copy(out, resp.Body)
  return err
}

Knowing the folder and name, you can configure nginx to distribute statics and receive files through it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question