Skip to content

File Upload

go-zero exposes the raw http.Request inside every handler, so standard Go multipart handling applies.

service upload-api {
@handler UploadFile
post /upload/file returns (UploadResp)
}
type UploadResp {
Filename string `json:"filename"`
Size int64 `json:"size"`
}
internal/handler/uploadfilehandler.go
func UploadFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(32 << 20); err != nil {
httpx.Error(w, err)
return
}
file, header, err := r.FormFile("file")
if err != nil {
httpx.Error(w, err)
return
}
defer file.Close()
dst, _ := os.Create("./uploads/" + filepath.Base(header.Filename))
defer dst.Close()
size, _ := io.Copy(dst, file)
httpx.OkJson(w, &types.UploadResp{Filename: header.Filename, Size: size})
}
}
Terminal window
curl -X POST http://localhost:8888/upload/file -F "file=@photo.png"
# {"filename":"photo.png","size":204800}
MaxBytes: 33554432 # 32 MB