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)
// 将文件内容转换为base64格式
fileContentBase64 := base64.StdEncoding.EncodeToString(fileContent)
return fileContentBase64, nil