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

如何从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.

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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格