mirror of
https://gitee.com/johng/gf
synced 2026-06-26 09:27:31 +08:00
51897b6e90cc57d7e0ea77abea529dea37036619
当前问题,使用嵌套字段时无法自动绑定到嵌套字段的文件 【已解决】
```
type TestData struct {
ID int64 `json:"id" dc:"ID"`
Name string `json:"name" dc:"Name"`
File *ghttp.UploadFile `json:"file" dc:"File" type:"file"`
Files *ghttp.UploadFiles `json:"files" dc:"Files" type:"file"`
}
type TestReq struct {
g.Meta `path:"/v1/admin/user/test" tags:"AdminUser" method:"POST" summary:"Test"`
ID int64 `json:"id" dc:"ID"`
Data TestData `json:"data" dc:"Data"`
File *ghttp.UploadFile `json:"file" dc:"File" type:"file"`
}
```
使用 multipart/form-data 上传时
```
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="data[id]"
11111111111111112
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="data[name]"
11111111111111112
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="data[description]"
11111111111111112
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="data[file]"; filename="xxxx.jpg"
Content-Type: image/jpeg
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="file"; filename="xxxxxr.jpg"
Content-Type: image/jpeg
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="data[files][]"; filename="debug.skk.moe_1732736392647.png"
Content-Type: image/png
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="data[files][]"; filename="85ee46523adb6a8ee4bf95795c91bef28e24983ed38afbec6789fb5077d75e3f.jpg"
Content-Type: image/jpeg
------WebKitFormBoundarypwjjDUNvfZkxxlhH
Content-Disposition: form-data; name="id"
1000
------WebKitFormBoundarypwjjDUNvfZkxxlhH--
```
【问题描述】
之前使用:
```
var (
request = g.RequestFromCtx(ctx)
)
var data = request.GetRequestMap()
```
获得的结果是扁平化的:
```
{
"data": {
"description": "11111111111111112",
"id": "11111111111111112",
"name": "11111111111111112"
},
"data[file]": {
"Filename": "xxxxx.jpg",
},
"data[files][]": [
{
"Filename": "xxx.png",
},
{
"Filename": "xxx.jpg",
}
],
"file": {
"Filename": "xxxx.jpg",
"Size": 5252553
},
"id": "1000"
}
```
由于没有将 `map["data[file]"]` 以 `map["data"]["file"]` 的形式存储,导致最终进行 `r.Parse` 时无法将文件正确绑定到结构体字段:
```
File *ghttp.UploadFile `json:"file" dc:"File" type:"file"`
Files *ghttp.UploadFiles `json:"files" dc:"Files" type:"file"`
```
这些字段无论如何都是 nil。
【解决方案】
现在已修复此问题,通过解析嵌套的字段名并构建正确的嵌套Map结构。修复后,`GetRequestMap()` 返回的结果如下:
```
{
"data": {
"description": "11111111111111112",
"id": "11111111111111112",
"name": "11111111111111112",
"file": {
"Filename": "xxxxx.jpg",
},
"files[]": [
{
"Filename": "xxx.png",
},
{
"Filename": "xxx.jpg",
}
]
},
"file": {
"Filename": "xxxx.jpg",
"Size": 5252553
},
"id": "1000"
}
```
这样,嵌套结构中的文件字段现在可以正确绑定到相应的结构体字段了。这一修复实现了对表单中嵌套文件字段的完整支持。
refactor(util/gconv): add
Converter feature for more flexable and extensible type converting (#4107)
fix(net/ghttp): json omitempty takes no effect in
BuildParams, which is not compatible with old version (#4041)
fix(database/gdb): move
Raw parameter from args to sql statement before committed to db driver (#3997)
A powerful framework for faster, easier, and more efficient project development.
Documentation
- GoFrame Official Site: https://goframe.org
- GoFrame Official Site(en): https://goframe.org/en
- GoFrame Mirror Site(中文): https://goframe.org.cn
- GoFrame Mirror Site(github pages): https://pages.goframe.org
- GoDoc API: https://pkg.go.dev/github.com/gogf/gf/v2
Contributors
💖 Thanks to all the contributors who made GoFrame possible 💖
License
GoFrame is licensed under the MIT License, 100% free and open-source, forever.
Languages
GO
100%
