作者 yangfu

上传修改

1 package v1 1 package v1
2 2
3 import ( 3 import (
4 - "fmt"  
5 "opp/controllers" 4 "opp/controllers"
6 "opp/protocol" 5 "opp/protocol"
7 "opp/services/upload" 6 "opp/services/upload"
@@ -25,19 +24,16 @@ func (this *UploadController) Image() { @@ -25,19 +24,16 @@ func (this *UploadController) Image() {
25 this.Resp(msg) 24 this.Resp(msg)
26 }() 25 }()
27 var request = &protocol.FileRequest{} 26 var request = &protocol.FileRequest{}
28 - for i := 1; i <= 9; i++ { 27 + if len(this.Ctx.Request.MultipartForm.File) != 0 {
29 var tmp *protocol.FileResponse 28 var tmp *protocol.FileResponse
30 - key := fmt.Sprintf("%v%v", "image", i)  
31 - if request.Files, err = this.GetFiles(key); err != nil {  
32 - log.Error(key, err)  
33 - err = nil  
34 - break 29 + for _, v := range this.Ctx.Request.MultipartForm.File {
  30 + request.Files = v
  31 + if tmp, err = upload.Image(request); err != nil {
  32 + log.Error(err)
  33 + return
  34 + }
  35 + rsp.Paths = append(rsp.Paths, tmp.Paths...)
35 } 36 }
36 - if tmp, err = upload.Image(request); err != nil {  
37 - log.Error(err)  
38 - return  
39 - }  
40 - rsp.Paths = append(rsp.Paths, tmp.Paths...)  
41 } 37 }
42 msg = protocol.NewReturnResponse(rsp, err) 38 msg = protocol.NewReturnResponse(rsp, err)
43 } 39 }
@@ -54,19 +50,16 @@ func (this *UploadController) Voice() { @@ -54,19 +50,16 @@ func (this *UploadController) Voice() {
54 this.Resp(msg) 50 this.Resp(msg)
55 }() 51 }()
56 var request = &protocol.FileRequest{} 52 var request = &protocol.FileRequest{}
57 - for i := 1; i <= 9; i++ { 53 + if len(this.Ctx.Request.MultipartForm.File) != 0 {
58 var tmp *protocol.FileResponse 54 var tmp *protocol.FileResponse
59 - key := fmt.Sprintf("%v%v", "voice", i)  
60 - if request.Files, err = this.GetFiles(key); err != nil {  
61 - log.Error(key, err)  
62 - err = nil  
63 - break  
64 - }  
65 - if tmp, err = upload.Voice(request); err != nil {  
66 - log.Error(err)  
67 - return 55 + for _, v := range this.Ctx.Request.MultipartForm.File {
  56 + request.Files = v
  57 + if tmp, err = upload.Voice(request); err != nil {
  58 + log.Error(err)
  59 + return
  60 + }
  61 + rsp.Paths = append(rsp.Paths, tmp.Paths...)
68 } 62 }
69 - rsp.Paths = append(rsp.Paths, tmp.Paths...)  
70 } 63 }
71 msg = protocol.NewReturnResponse(rsp, err) 64 msg = protocol.NewReturnResponse(rsp, err)
72 } 65 }
@@ -77,16 +70,22 @@ func (this *UploadController) Video() { @@ -77,16 +70,22 @@ func (this *UploadController) Video() {
77 var ( 70 var (
78 msg *protocol.ResponseMessage 71 msg *protocol.ResponseMessage
79 err error 72 err error
  73 + rsp *protocol.FileResponse = &protocol.FileResponse{}
80 ) 74 )
81 defer func() { 75 defer func() {
82 this.Resp(msg) 76 this.Resp(msg)
83 }() 77 }()
84 - for i := 1; i <= 9; i++ {  
85 - var request = &protocol.FileRequest{}  
86 - if request.Files, err = this.GetFiles("file"); err != nil {  
87 - log.Error(err)  
88 - return 78 + var request = &protocol.FileRequest{}
  79 + if len(this.Ctx.Request.MultipartForm.File) != 0 {
  80 + var tmp *protocol.FileResponse
  81 + for _, v := range this.Ctx.Request.MultipartForm.File {
  82 + request.Files = v
  83 + if tmp, err = upload.Video(request); err != nil {
  84 + log.Error(err)
  85 + return
  86 + }
  87 + rsp.Paths = append(rsp.Paths, tmp.Paths...)
89 } 88 }
90 - msg = protocol.NewReturnResponse(upload.Video(request))  
91 } 89 }
  90 + msg = protocol.NewReturnResponse(rsp, err)
92 } 91 }
@@ -20,15 +20,15 @@ import ( @@ -20,15 +20,15 @@ import (
20 //上传图片 20 //上传图片
21 func Image(request *protocol.FileRequest) (rsp *protocol.FileResponse, err error) { 21 func Image(request *protocol.FileRequest) (rsp *protocol.FileResponse, err error) {
22 var () 22 var ()
23 - for i := range request.Files {  
24 - f := request.Files[i]  
25 - subfix := path.Ext(f.Filename)  
26 - //文件格式不符合  
27 - if !(subfix == ".jpg" || subfix == ".gif" || subfix == ".png") {  
28 - err = common.NewErrorWithMsg(2, "file format error")  
29 - return  
30 - }  
31 - } 23 + //for i := range request.Files {
  24 + //f := request.Files[i]
  25 + //subfix := path.Ext(f.Filename)
  26 + //文件格式不符合
  27 + //if !(subfix == ".jpg" || subfix == ".gif" || subfix == ".png") {
  28 + // err = common.NewErrorWithMsg(2, "file format error")
  29 + // return
  30 + //}
  31 + //}
32 request.FileType = protocol.FileImage 32 request.FileType = protocol.FileImage
33 return UploadFile(request) 33 return UploadFile(request)
34 } 34 }