Home >Backend Development >Golang >Get image from folder with imageurl in database and display that image in my browser using echo in golang

Get image from folder with imageurl in database and display that image in my browser using echo in golang

王林
王林forward
2024-02-11 12:39:081158browse

从数据库中带有 imageurl 的文件夹获取图像,并在我的浏览器中使用 golang 中的 echo 显示该图像

php Xiaobian Yuzai will introduce you in this article how to get the image from the folder with imageurl in the database and display it in the browser using echo in golang the image. This process involves querying the imageurl from the database and then using the echo library in golang to send the image data to the browser for display. Through the guidance of this article, you will be able to easily implement this function and add a richer image display effect to your website or application.

Question content

I get the data from the database and they display the same in browser or software like postman

/ 20221125143847
// http://localhost:3000/api/v1/datesmain/read

{

"alldates": [
{
  "id": 1,
  "createdat": "2022-11-25t00:00:00z",
  "updatedat": "2022-11-25t00:00:00z",
  "deletedat": null,
  "volume": "20",
  "image_url": "/assets/images/img_3429.jpg\n",
  "average_weight": 7.5,
  "dates_type_id": 1,
  "wight_type_id": 1
},
{
  "id": 2,
  "createdat": "2022-11-25t00:00:00z",
  "updatedat": "2022-11-25t00:00:00z",
  "deletedat": null,
  "volume": "15",
  "image_url": "/assets/images/img_3436.jpg\n",
  "average_weight": 7.5,
  "dates_type_id": 1,
  "wight_type_id": 1
}
  ]
}

As you can see the image urls are correct and all the images are located in a folder under the root of the project with the name "assets" Because I don't understand it's possible to display my image through this url

http://localhost:3000/api/v1/datesmain/assets/images/img_3429.jpg

I want to display them all as a list view on the front end using java or...

But when copying and pasting a URL in the browser like this

// http://localhost:3000/api/v1/datesmain/static/images/img_3429.jpg
{
"message": "not found"
}

How can I solve this problem?

This is my little code

func ReadAllDatesMain(e echo.Context) error {
ad, err := logic.ReadAllDatesMain()
if err != nil {
    return nil
}
return e.JSON(http.StatusOK, map[string]interface{}{
    "alldates": ad,
})

}

Solution

You have defined a route for the API, but not for the file. You need to set static routing in echo. You can then access the folder structure via URL from a browser or a client such as Postman.

The above is the detailed content of Get image from folder with imageurl in database and display that image in my browser using echo in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete