resource.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package controllers
  2. import (
  3. "eta/eta_crawler/models"
  4. "os"
  5. )
  6. type ResourceController struct {
  7. BaseAuthController
  8. }
  9. // ResourceUpload 上传文件
  10. // @Title 上传文件
  11. // @Description 上传文件
  12. // @Param MenuId query int true "目录ID"
  13. // @Param File query file true "文件"
  14. // @Success 200 Ret=200 操作成功
  15. // @router /resource/upload [post]
  16. func (this *ResourceController) ResourceUpload() {
  17. br := new(models.BaseResponse).Init()
  18. defer func() {
  19. this.Data["json"] = br
  20. this.ServeJSON()
  21. }()
  22. f, h, e := this.GetFile("file")
  23. if e != nil {
  24. br.Msg = "获取资源信息失败"
  25. br.ErrMsg = "获取资源信息失败, Err:" + e.Error()
  26. return
  27. }
  28. defer func() {
  29. _ = f.Close()
  30. }()
  31. uploadDir := "./static/"
  32. //uploadDir := "/Users/xi/Desktop/file"
  33. if e = os.MkdirAll(uploadDir, 766); e != nil {
  34. br.Msg = "存储目录创建失败"
  35. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  36. return
  37. }
  38. //ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
  39. filePath := uploadDir + "/" + h.Filename
  40. if e = this.SaveToFile("file", filePath); e != nil {
  41. br.Msg = "文件保存失败"
  42. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  43. return
  44. }
  45. //defer func() {
  46. // _ = os.Remove(filePath)
  47. //}()
  48. br.Msg = "上传成功"
  49. br.Ret = 200
  50. br.Success = true
  51. }