gpt4 book ai didi

go - 如何在 golang 中从客户端接收 multipart?

转载 作者:数据小太阳 更新时间:2023-10-29 03:32:45 31 4
gpt4 key购买 nike

这里是我的代码和输出:

func RestClient(req *http.Request) {
fmt.Println("main (120):::", req.MultipartForm.File)
}

main (120)::: &{map[userName:[0xc4200a66e0] diamond:[0xc4200f67b0] ]}

for k,v := range req.MultipartForm.File{
if k == "userName" {
for _, v2 := range v {
fmt.Println("main (130):::",v2)
}
}
}
 > main (130)::: &{ map[Content-Length:[8]
> Content-Disposition:[form-data; name="dk"]
> Content-Transfer-Encoding:[binary] Content-Type:[multipart/form-data;
> charset=utf-8]] 8 [117 115 101 114 78 97 109 101] }

我想要 (slice byte [117 115 101 114 78 97 109 101]) 但无法提取,如何打印 (content []byte),正如我们从 FileHeader 结构中知道的字段:

type FileHeader struct {
Filename string
Header textproto.MIMEHeader
Size int64

content []byte
tmpfile string
}

提前致谢。

最佳答案

调用FileHeader.Open ,它返回一个 multipart.File :

type File interface {
io.Reader
io.ReaderAt
io.Seeker
io.Closer
}

你如何处理这个文件取决于你想对内容做什么。 ioutil.ReadAll 是一个选项,但通常将其保留原样并使用 io.Copy 将内容写入另一个 io.Writer 会更方便。阅读完后不要忘记调用 Close。

关于go - 如何在 golang 中从客户端接收 multipart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49667405/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com