upload_controller.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package controller
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "mime/multipart"
  5. "path/filepath"
  6. "xps/constant"
  7. services "xps/service"
  8. "xps/utils"
  9. )
  10. type FileData struct {
  11. FileInfo *multipart.FileHeader
  12. FileData *multipart.File
  13. }
  14. type UploadController struct {
  15. Base
  16. Service services.UploadService
  17. }
  18. func (c *UploadController) BeforeActivation(b mvc.BeforeActivation) {
  19. b.Handle("POST", "/file", "UploadFile")
  20. b.Handle("POST", "/files", "UploadFiles")
  21. }
  22. // UploadFile :Single Upload
  23. func (c *UploadController) UploadFile() *JsonResult {
  24. ocId := c.GetCurOcId()
  25. userId := c.GetCurUserId()
  26. f, fh, err := c.Ctx.FormFile("uploadfile")
  27. if err != nil {
  28. return ResultErr(err.Error(), nil)
  29. }
  30. defer f.Close()
  31. _, err = c.Ctx.SaveFormFile(fh, filepath.Join("./public/uploads/temp", fh.Filename))
  32. if err != nil {
  33. return ResultErr(err.Error(), nil)
  34. }
  35. fileFullPath := filepath.Join("./public/uploads/temp/", fh.Filename)
  36. targetFile := services.TargetFile{
  37. FileName: fh.Filename,
  38. FilePath: fileFullPath,
  39. }
  40. file, err := c.Service.UploadFile(ocId, userId, constant.DOC_DIR_TEMP, targetFile)
  41. if err != nil {
  42. return ResultErr(err.Error(), nil)
  43. } else {
  44. return ResultOk("获取成功", file)
  45. }
  46. }
  47. // UploadFiles :Multiple Upload
  48. func (c *UploadController) UploadFiles() *JsonResult {
  49. _, files, err := utils.UploadMultiFiles(c.Ctx)
  50. if err != nil {
  51. return ResultErr(err.Error(), nil)
  52. } else {
  53. return ResultOk("获取成功", files)
  54. }
  55. }