0

解析多部分表单时出错。做了以下尝试,但都没有解决: 不知道Golang是不是不支持rfc 1867。谢谢大家的帮助。

POST /uploadFile.htm HTTP/1.1
Host: myserver
Connection: keep-alive
Content-Length: 368
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="file"
Content-Type: multipart/form-data; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif

...contents of file2.gif...
--BbC04y--
--AaB03x--
err := request.ParseMultipartForm(1024)
log.Printf("value %v", request.MultipartForm.Value)
log.Printf("file %v", request.MultipartForm.File)

如果您查看日志,请查看以下内容

 value map[file:[--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif

...contents of file2.gif...
--BbC04y--]
file map[]

我应该怎么做才能解析多部分表单?

4

1 回答 1

0
import "mime/multipart"
...
mr := multipart.NewReader(r.Body, boundary)
for {
    p, err := mr.NextPart()
    if err == io.EOF {
        break
    }
...

您现在可以在每个部分之后记录 p 以检查格式并提取数据

于 2021-03-23T08:50:21.670 回答