You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
643 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package file
import (
"encoding/base64"
"os"
"path/filepath"
)
// 打开文件将对应文件转换为base64格式后返回前端
func GetFile(userName, filePath string) (string, error) {
// 获取main.go所在的目录
currentDir, err := os.Getwd()
if err != nil {
return "", err
}
baseDir := filepath.Join(currentDir, "file_library")
fullFilePath := filepath.Join(baseDir, userName, filePath)
fileContent, err := os.ReadFile(fullFilePath)
if err != nil {
return "", err
}
// 将文件内容转换为base64格式
fileContentBase64 := base64.StdEncoding.EncodeToString(fileContent)
return fileContentBase64, nil
}