From 608d726e54923c8be0134da550ca9cea8424e810 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 12 Apr 2024 17:25:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=97=A0=E6=96=87=E4=BB=B6=E5=93=8D=E5=BA=94?= =?UTF-8?q?json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/utils/file/file.go | 4 ++-- src/modules/common/controller/file.go | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/framework/utils/file/file.go b/src/framework/utils/file/file.go index d0a602ad..51854149 100644 --- a/src/framework/utils/file/file.go +++ b/src/framework/utils/file/file.go @@ -177,13 +177,13 @@ func ReadUploadFileStream(filePath, headerRange string) (map[string]any, error) "range": "", "chunkSize": 0, "fileSize": 0, - "data": nil, + "data": []byte{}, } // 文件大小 fileSize := getFileSize(fileAsbPath) if fileSize <= 0 { - return result, nil + return result, fmt.Errorf("file does not exist") } result["fileSize"] = fileSize diff --git a/src/modules/common/controller/file.go b/src/modules/common/controller/file.go index 35062434..c251c2ed 100644 --- a/src/modules/common/controller/file.go +++ b/src/modules/common/controller/file.go @@ -43,13 +43,6 @@ func (s *FileController) Download(c *gin.Context) { return } routerPath := string(decodedBytes) - // 地址文件名截取 - fileName := routerPath[strings.LastIndex(routerPath, "/")+1:] - - // 响应头 - c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+url.QueryEscape(fileName)+`"`) - c.Writer.Header().Set("Accept-Ranges", "bytes") - c.Writer.Header().Set("Content-Type", "application/octet-stream") // 断点续传 headerRange := c.GetHeader("Range") @@ -58,6 +51,12 @@ func (s *FileController) Download(c *gin.Context) { c.JSON(200, result.ErrMsg(err.Error())) return } + + // 响应头 + c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+url.QueryEscape(filepath.Base(routerPath))+`"`) + c.Writer.Header().Set("Accept-Ranges", "bytes") + c.Writer.Header().Set("Content-Type", "application/octet-stream") + if headerRange != "" { c.Writer.Header().Set("Content-Range", fmt.Sprint(resultMap["range"])) c.Writer.Header().Set("Content-Length", fmt.Sprint(resultMap["chunkSize"])) @@ -65,7 +64,6 @@ func (s *FileController) Download(c *gin.Context) { } else { c.Writer.Header().Set("Content-Length", fmt.Sprint(resultMap["fileSize"])) c.Status(200) - } c.Writer.Write(resultMap["data"].([]byte)) }