Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

html - How can I save images in the database and display them on the site?

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"

question from:https://stackoverflow.com/questions/65641598/how-can-i-save-images-in-the-database-and-display-them-on-the-site

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...