get image from folder with imageurl in database and show that in my browser with echo in golang

huangapple go评论73阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年11月26日 04:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/74577746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定