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.

57 lines
1.4 KiB

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"
"fmt"
"os"
"path/filepath"
)
func FileSave(userName, savePath, fileDataBase64 string) error {
currentDir, err := os.Getwd()
if err != nil {
return err
}
baseDir := filepath.Join(currentDir, "file_library")
fullFilePath := filepath.Join(baseDir, userName, savePath)
// 创建文件所在目录(如果不存在)
dir := filepath.Dir(fullFilePath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
// 记录开始解码
fmt.Println("开始对文件数据进行Base64解码...")
fileData, err := base64.StdEncoding.DecodeString(fileDataBase64)
if err != nil {
// 记录解码失败及错误信息
fmt.Printf("Base64解码失败错误信息%v\n", err)
return err
}
// 记录解码成功
fmt.Println("Base64解码成功")
// 创建目标文件前记录日志
fmt.Printf("准备创建文件:%s\n", fullFilePath)
targetFile, err := os.Create(fullFilePath)
if err != nil {
// 记录创建文件失败及错误信息
fmt.Printf("创建文件失败,错误信息:%v\n", err)
return err
}
defer targetFile.Close()
// 写入文件内容前记录日志
fmt.Println("准备写入文件内容")
_, err = targetFile.Write(fileData)
if err != nil {
// 记录写入文件内容失败及错误信息
fmt.Printf("写入文件内容失败,错误信息:%v\n", err)
return err
}
fmt.Println("文件内容写入成功")
return nil
}