From c98e24783da4cca84508f8f2ba7ec68bbe1669f7 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sun, 14 May 2023 15:05:38 +0800 Subject: [PATCH] fix --- upload/base_upload.go | 1 + upload/chunk_upload.go | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/upload/base_upload.go b/upload/base_upload.go index 0305701..12074d4 100644 --- a/upload/base_upload.go +++ b/upload/base_upload.go @@ -62,6 +62,7 @@ func (a *BaseClient) Upload(opts ...OptionsSetter) Client { FileName: options.Result.FileName, CurrentSize: uint64(options.Result.FileSize), } + a.Context.Request().MultipartForm() info.Init(func(name string) string { return a.Form(name) }, a.Header) diff --git a/upload/chunk_upload.go b/upload/chunk_upload.go index c7e184a..f240356 100644 --- a/upload/chunk_upload.go +++ b/upload/chunk_upload.go @@ -16,6 +16,10 @@ import ( "github.com/webx-top/echo" ) +const ( + defaultMaxMemory int64 = 2 << 20 // 2 MB +) + // 分片上传 func (c *ChunkUpload) Upload(r *http.Request, opts ...ChunkInfoOpter) (int64, error) { info := &ChunkInfo{ @@ -24,21 +28,23 @@ func (c *ChunkUpload) Upload(r *http.Request, opts ...ChunkInfoOpter) (int64, er for _, opt := range opts { opt(info) } - info.Init(r.FormValue, r.Header.Get) - if !c.IsSupported(info) { - return 0, ErrChunkUnsupported - } + maxMemory := defaultMaxMemory if c.FileMaxBytes > 0 { if r.ContentLength > int64(c.FileMaxBytes) { return 0, fmt.Errorf(`%w: %d>%d `, ErrRequestBodyExceedsLimit, r.ContentLength, c.FileMaxBytes) } - if r.MultipartForm == nil { - err := r.ParseMultipartForm(int64(c.FileMaxBytes)) - if err != nil { - return 0, fmt.Errorf("上传文件错误: %w", err) - } + maxMemory = int64(c.FileMaxBytes) + } + if r.MultipartForm == nil { + err := r.ParseMultipartForm(maxMemory) + if err != nil { + return 0, fmt.Errorf("上传文件错误: %w", err) } } + info.Init(r.FormValue, r.Header.Get) + if !c.IsSupported(info) { + return 0, ErrChunkUnsupported + } // 获取上传文件 upFile, fileHeader, err := r.FormFile(info.FormField) if err != nil {