最近开发日历记事本V2版本(Gin+vue3)用到了,记录一下
Vditor图片上传官方文档:文档
func (a *FileApi) UploadFiles(c *gin.Context) {
userInfo, _ := base.GetCurrentUserInfo(c)
configUpload := global.Config.GetValueString("base", "source_path")
form, err := c.MultipartForm()
if err != nil {
apiReturn.Error(c, "上传失败")
return
}
files := form.File["files[]"]
errFiles := []string{}
succMap := map[string]string{}
for _, f := range files {
fileExt := strings.ToLower(path.Ext(f.Filename))
fileName := cmn.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
fildDir := fmt.Sprintf("%s/%d/%d/%d/", configUpload, time.Now().Year(), time.Now().Month(), time.Now().Day())
isExist, _ := cmn.PathExists(fildDir)
if !isExist {
os.MkdirAll(fildDir, os.ModePerm)
}
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
if c.SaveUploadedFile(f, filepath) != nil {
errFiles = append(errFiles, f.Filename)
} else {
// 成功
// 像数据库添加记录
mFile := models.File{}
mFile.AddFile(userInfo.ID, f.Filename, filepath)
succMap[f.Filename] = filepath[1:]
}
}
apiReturn.SuccessData(c, gin.H{
"succMap": succMap,
"errFiles": errFiles,
})
}
正在学习Go语言的PHP程序员。