Jelajahi Sumber

feat: oss测试

zhangxinlei1996 1 tahun lalu
induk
melakukan
b67418e7b0
1 mengubah file dengan 14 tambahan dan 1 penghapusan
  1. 14 1
      entity/oss.go

+ 14 - 1
entity/oss.go

@@ -4,7 +4,9 @@ import (
 	"crypto/sha256"
 	"fmt"
 	"io"
+	"net/url"
 	"os"
+	"path/filepath"
 
 	"app.yhyue.com/moapp/jybase/common"
 	"github.com/zeromicro/go-zero/core/logx"
@@ -32,8 +34,12 @@ func OssPutObject(objectName string, fd io.Reader, ossBucketName string) (bool,
 		logx.Error("Error:", err)
 		return false, err
 	}
+	fileNameOnly := getFileNameOnly(objectName)
+	fileExtension := TypeByExt(objectName)
+
 	option := []oss.Option{
-		oss.ContentDisposition("attachment;filename=" + objectName),
+		// oss.ContentDisposition("attachment;filename=" + objectName),
+		oss.ContentDisposition("attachment;filename=" + url.QueryEscape(fileNameOnly) + "." + fileExtension + ";filename*=UTF-8''" + url.QueryEscape(fileNameOnly) + "." + fileExtension),
 		oss.ContentType("application/octet-stream"),
 		oss.ACReqHeaders("Access-Control-Allow-Origin"),
 		oss.ACReqMethod("*"),
@@ -83,3 +89,10 @@ func TypeByExt(path string) string {
 	}
 	return ""
 }
+
+// 获取文件名称(不带后缀)
+func getFileNameOnly(filename string) string {
+	extension := filepath.Ext(filename)
+	fileNameOnly := filename[0 : len(filename)-len(extension)]
+	return fileNameOnly
+}