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

如何从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条回答 默认 最新

  • dpxua26604 2019-08-13 05:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器