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

如何从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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题