简体   繁体   中英

How to get images path from golang project file

I am a beginner in making a REST full API using golang, what I want to ask is to get an image file from the path in my golang project, before I have successfully uploaded the image and saved it in the path and the path I have saved it in mysql database, how do I do it to be able to display the image in the form of a link, so I can load it on android,

and how to make it like this "http: // localhost: 3004 / images / imagename.jpg" inside the json result ( ImgEvent )??

please help me... thanks

func (idb *InDB) GetEvents(c *gin.Context) {
    var (
        events    []structs.Event
        newEvents structs.Event
        result    gin.H
    )

    getimage := "img_event"
    data := idb.DB.Select(getimage).Find(&events)
    if data != nil {
        result = gin.H{
            "message": "Not found",
        }
    }
    newEvents.ImgEvent = getimage

    data = idb.DB.Find(&events)

    if len(events) <= 0 {
        result = gin.H{
            "result": nil,
            "count":  0,
        }
    } else {
        result = gin.H{
            "data": events,
        }
    }

    c.JSON(http.StatusOK, result)
}

========= Respone Json ===========

{
    "data": [
        {
            "ID": 1,
            "CreatedAt": "2019-07-28T22:38:20Z",
            "UpdatedAt": "2019-08-12T09:51:41Z",
            "DeletedAt": null,
            "Judul": "test1",
            "Isi": "Dalam menyambut ulang tahun Accent-er Regional JawaBarat yang ke 3.\r\n\r\nKami mengundang seluruh member terdaftar maupun non member untuk ikut hadir meramaikan acara ini.\r\n\r\nYang akan di selenggarakan di\r\n\r\nCikole,Lembang Bandung
            "ImgEvent": "event-images/event-bukalapak.jpg",
            "TotalComments": 1,
            "TotalLikes": 5,
            "TotalView": 3
        },

You should a create a file server to be able create an http link for files. You can use net/http's Fileserver for the same. Fileserver is a good choice if you want it for a small time, like as long as you API server is running. Another choice would be to create an FTP server and use that to store images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM