-1

There is a field in the database table that receives images in blob format. How can this be displayed on the site? The main goal is to send images to the database, and display them in the website.It would be great if you gave an example of the go code this is insert data code:

ins, err := db.Query(fmt.Sprintf("INSERT INTO `photo` (`photo`)" +" VALUES('%s')", img))
if err != nil {
    panic(err)
}
defer ins.Close()

attempt to display an image(saving in a variable):

vars := mux.Vars(r)
res, err := db.Query(fmt.Sprintf("SELECT * FROM `photo` WHERE `id` = '%s'", vars["id"]))
if err != nil {
    panic(err)
}
    
showPhoto = Photo{}
for res.Next() {
    var post Photo
    err = res.Scan(&post.Id, &post.Img)
    if err != nil {
        panic(err)
    }
    encodeImg, err := b64.StdEncoding.DecodeString(post.Img)
    showPhoto = post
}

several files are sent from one input, so the terminal displays the error " 1 variable, but in base64.stdencoding.decodeString returns 2 values"

Merrin K
  • 1,602
  • 1
  • 16
  • 27
righty_dev
  • 27
  • 5
  • Does this answer your question? [Displaying BLOB image from Mysql database into dynamic div in html](https://stackoverflow.com/questions/34111390/displaying-blob-image-from-mysql-database-into-dynamic-div-in-html) – adampweb Jan 09 '21 at 10:37
  • Hi can you share what you got so far (researches, unworking code, etc). Please provide a "Minimum working example" Thank you. – Oleg Butuzov Jan 09 '21 at 10:37
  • This is not a proper question because it lacks a direct problem. Basically you're asking for a complete solution but SO is not a service for hiring unpaid freelancers. Please start with decomposing the problem to digestible parts (How to store and fetch image blobs from the chosen DBMS? How to include BLOB directly in an HTML document to make it displayable as an image? If that is not possible, what alternatives may exist? (They do exist.)) Then draft the solution for each _part_ of the problem. Make each work in isolation. Then make them work together. – kostix Jan 09 '21 at 10:52
  • Basically that's a standard approach to solve any problem at all. I would recommend to read [this classic piece](https://whathaveyoutried.com/) thoroughly and ponder the advice it gives. To understand why SO is targeted at solving _narrow, particular_ problems, please read [this](https://stackoverflow.com/help/on-topic). Your question does in any way present a narrowly-scoped problem, and hence has nothing to with Go—as of now. – kostix Jan 09 '21 at 10:52
  • A couple pointers: 1) [Images in HTML](https://www.google.com/search?q=html+embed+image); 2) [`html/template`](https://golang.org/pkg/html/template/); 3) Working with [`database/sql`](http://go-database-sql.org/). – kostix Jan 09 '21 at 10:57
  • @kostix I know how to output data from the database to the site, my problem specifically lies in the display of images – righty_dev Jan 09 '21 at 11:24
  • @AdamP. thanks for the answer, but this is only for one image, and besides I need an answer on go – righty_dev Jan 09 '21 at 11:26
  • Do not use Sprintf to construct database queries. This is vulnerable to [injections](https://owasp.org/www-project-top-ten/). See [the examples in the sql package documentation](https://golang.org/pkg/database/sql/#pkg-examples) for how to execute queries safely. – Peter Jan 10 '21 at 11:57
  • @Peter ok, thanks, but how can I display images in html? – righty_dev Jan 10 '21 at 12:12

2 Answers2

0

You should never ever save the images directly into the database. The size of your database will increase a lot. You should save all of your images on the server, and save to the database only the path to that image file.

Another solution, which is actually great and easy to implement is using DigitalOcean Spaces It is really cheap for what you get.

Maybe you can search for some articles on the internet to see why you should not store images directly to the database, like this one

Daniel Sava
  • 250
  • 3
  • 12
0

sample solition: save photo as file in any directory. and save path of that image in database. then just show that image in html as:<img src="/static/{{.}}"/>

Don't forget the static file server. Images must be submitted on the same endpoint, something like this: app.StaticFiles("/static", "path/to/imagesDir")

Ahmed
  • 367
  • 2
  • 12
  • thanks, but how can i save a path of image in DB(multiple file input)? – righty_dev Jan 10 '21 at 10:53
  • what is a package of "app.StaticFiles"? – righty_dev Jan 10 '21 at 10:56
  • save path as any string in database. multiy file is just multy strings. for example: ```Query("insert into myTable("photo1" "photo2", "photo3") values("dog.jpg", "cat.jpg", "monkey.jpg")``` – Ahmed Jan 10 '21 at 19:21
  • ```app.StaticFiles``` This varies according to the library you use. for example in standard library you will find somthing like : ```http.FileServer(http.Dir("./Static"))``` – Ahmed Jan 10 '21 at 19:40