From 500985cf7556808ee42e2e9e3066bb4648a8e534 Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期二, 18 六月 2024 14:28:16 +0800 Subject: [PATCH] watch preloads file to autoload --- controllers/audio.go | 18 +++++++++++------- 1 files changed, 11 insertions(+), 7 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index 0717d9b..8f5f108 100644 --- a/controllers/audio.go +++ b/controllers/audio.go @@ -7,6 +7,7 @@ "gorm.io/gorm" "io" "mime/multipart" + "net/url" "os" "path" "path/filepath" @@ -28,14 +29,16 @@ // Upload // @Tags 闊抽 // @Summary 涓婁紶闊抽 +// @Accept multipart/form-data // @Produce application/json -// @Param files formData []file false "澶氭枃浠朵笂浼�" +// @Param file formData []file false "澶氭枃浠朵笂浼�" // @Success 200 {object} util.Response "鎴愬姛" // @Router /api-sa/v1/audio/upload [post] func (slf AudioCtl) Upload(c *gin.Context) { var headers []*multipart.FileHeader - if len(c.Request.MultipartForm.File["file"]) > 1 { - headers = c.Request.MultipartForm.File["file"] + files, _ := c.MultipartForm() + if len(files.File["file"]) > 1 { + headers = files.File["file"] } else { util.ResponseFormat(c, code.RequestParamError, "鏂囦欢闇�瑕佷竴涓�瀵瑰簲") return @@ -89,7 +92,7 @@ //瑙f瀽 浜よ矾鍙�:123_鍏噷鏍�:321 fileds := string(bts) arr = strings.Split(fileds, "_") - if len(arr) > 1 { + if len(arr) != 2 { util.ResponseFormat(c, code.RequestParamError, "鏂囦欢鍐呭鏍煎紡涓嶅") return } else { @@ -291,8 +294,9 @@ } if params.Filetype == 2 { filepath = audio.TxtFilePath - //璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷 - c.Header("Content-Type", "text/plain") + //璁剧疆Content-Type涓簍xt鏂囦欢绫诲瀷,閬垮厤涓枃涔辩爜 + c.Header("Content-Type", "text/plain; charset=utf-8") + c.Header("Content-Transfer-Encoding", "binary") } if filepath == "" { util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触") @@ -312,7 +316,7 @@ return } - c.Header("Content-Disposition", "inline; filename="+audio.Name) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� + c.Header("Content-Disposition", "inline; filename="+url.PathEscape(audio.Name)) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮� c.Header("Content-Length", fmt.Sprint(fileInfo.Size())) if _, err := io.Copy(c.Writer, file); err != nil { -- Gitblit v1.8.0