func getFileExt(r *revel.Request, formField string) (bool, string) { file, header, err := r.FormFile(formField) if err != nil { return false, "" } defer file.Close() return true, strings.ToLower(filepath.Ext(header.Filename)) }
/** *@return 保存的文件名称(不含后缀) *@return 上载的文件名称 *@return 上载的文件后缀 */ func saveFile(req *revel.Request, formFileName string, savepath string) (string, string, string) { saveFileName := strings.Replace(uuid.NewUUID().String(), "-", "", -1) file, fh, err := req.FormFile(formFileName) if err != nil { fmt.Println(err) } //获得文件后缀 suffix := path.Ext(fh.Filename) defer file.Close() //创建保存路径 os.MkdirAll(savepath, os.ModePerm) f, err1 := os.OpenFile(savepath+"\\"+saveFileName+suffix, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) if err1 != nil { fmt.Println(err1) } defer f.Close() io.Copy(f, file) return saveFileName, fh.Filename, suffix }
func saveFile(r *revel.Request, formField string) string { file, header, err := r.FormFile(formField) if err != nil { return "" } defer file.Close() uuid := strings.Replace(uuid.NewUUID().String(), "-", "", -1) ext := filepath.Ext(header.Filename) fileName := uuid + ext os.MkdirAll(uploadPath, 0777) f, err := os.OpenFile(uploadPath+fileName, os.O_WRONLY|os.O_CREATE, 0666) defer f.Close() if err != nil { fmt.Println(err) } else { io.Copy(f, file) } return fileName }