英文:
get image from folder with imageurl in database and show that in my browser with echo in golang
问题
我从数据库中获取数据,并在浏览器或类似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
}
]
}
如你所见,图片的URL是正确的,所有图片都在项目根目录下的名为"assets"的文件夹中。根据我的理解,可以通过以下URL显示我的图片:
http://localhost:3000/api/v1/datesmain/assets/images/IMG_3429.jpg
我想在前端使用Java或其他方式将它们全部显示为列表视图,但是当我将其中一个URL复制并粘贴到浏览器中时,如下所示:
// http://localhost:3000/api/v1/datesmain/static/images/IMG_3429.jpg
{
"message": "Not Found"
}
我该如何解决这个问题?
以下是我的简短代码:
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,
})
}
英文:
I get my data from database and they show in browser or software like postman the same as this
/ 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 se image url is currect and all images are in filder in root of project with the name of "assets"
as I undrestand its possible show my image by this URL
> http://localhost:3000/api/v1/datesmain/assets/images/IMG_3429.jpg
I want to show all of them as a listview in front end with java or ...
but when copy and past one of url in browser like this
> // http://localhost:3000/api/v1/datesmain/static/images/IMG_3429.jpg
{
"message": "Not Found"
}
How can I resolve that?
and this is my tiny 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,
})
}
答案1
得分: 0
你已经为你的API定义了路由,但没有为你的文件定义路由。你需要在Echo中设置静态路由。然后你可以通过客户端(如浏览器或Postman)的URL访问文件夹结构。
英文:
You have defined route for your API but not for your files. You need to set static route in echo. Then you can access the folder structure by URL from client like browser or postman.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论