Documentation
¶
Overview ¶
Multipart file uploads.
ctx.UploadFormFiles saves every file of the form into a directory. It sanitizes each client-supplied name against context.ValidFilenameRegexp (letters, digits, '.', '_', '-'), blocks path traversal such as "../../etc/crontab" and silently skips names it cannot make safe. The optional callback runs before each save, so it can reject or rename a file.
PostMaxMemory bounds how much of a multipart form is buffered in memory while parsing (32MB by default); the spillover goes to temporary files. It is NOT a body size limit - for that see ../body-limits.
The upload calls return their errors rather than answering the client, so the handlers forward them: errors.HandleRequestError for a malformed request, errors.HandleError for a failure that is the server's, and errors.InvalidArgument for a name that cannot be made safe. Each renders the rest/errors JSON envelope with the status its classification implies.
Learn: save multipart uploads under safe file names and know which error helper answers which kind of failure. Before: ../pagination
Run:
go run main.go
Try:
curl -i -X POST http://localhost:8080/upload -F 'files=@main.go' -F 'files=@go.mod' # 200, both names and the byte count curl -i -X POST http://localhost:8080/upload/one -F 'file=@main.go' # 200, one name and its size curl -i -X POST http://localhost:8080/upload -d 'not=multipart' # 400, not a multipart form
Next: ../body-limits