hinego 51897b6e90 feat(ghttp): 支持文件上传字段的嵌套结构解析
当前问题,使用嵌套字段时无法自动绑定到嵌套字段的文件 【已解决】

```
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"
}
```

这样,嵌套结构中的文件字段现在可以正确绑定到相应的结构体字段了。这一修复实现了对表单中嵌套文件字段的完整支持。
2025-04-07 17:53:27 +08:00
2025-03-17 15:52:26 +08:00
2025-03-17 15:52:26 +08:00
dev
2017-07-04 18:31:39 +08:00
2025-02-27 14:35:00 +08:00
2025-02-27 14:35:00 +08:00
2025-03-09 22:31:20 +08:00
2025-03-17 15:52:26 +08:00
2025-03-17 15:52:26 +08:00

goframe gf logo

Go Reference GoFrame CI Go Report Card Code Coverage Production Ready License

Release GitHub pull requests GitHub closed pull requests GitHub issues GitHub closed issues Stars Forks

A powerful framework for faster, easier, and more efficient project development.

Documentation

Contributors

💖 Thanks to all the contributors who made GoFrame possible 💖

goframe contributors

License

GoFrame is licensed under the MIT License, 100% free and open-source, forever.

Description
No description provided
Readme MIT 34 MiB
Languages
GO 100%