dongqixuan3112
2019-08-13 04:51
浏览 296
已采纳

如何从golang项目文件获取图像路径

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.

Kami mengundang seluruh member terdaftar maupun non member untuk ikut hadir meramaikan acara ini.

Yang akan di selenggarakan di

Cikole,Lembang Bandung
            "ImgEvent": "event-images/event-bukalapak.jpg",
            "TotalComments": 1,
            "TotalLikes": 5,
            "TotalView": 3
        },
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题