作者 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,20 +24,17 @@ func (this *UploadController) Image() { @@ -25,20 +24,17 @@ 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  
35 - } 29 + for _, v := range this.Ctx.Request.MultipartForm.File {
  30 + request.Files = v
36 if tmp, err = upload.Image(request); err != nil { 31 if tmp, err = upload.Image(request); err != nil {
37 log.Error(err) 32 log.Error(err)
38 return 33 return
39 } 34 }
40 rsp.Paths = append(rsp.Paths, tmp.Paths...) 35 rsp.Paths = append(rsp.Paths, tmp.Paths...)
41 } 36 }
  37 + }
42 msg = protocol.NewReturnResponse(rsp, err) 38 msg = protocol.NewReturnResponse(rsp, err)
43 } 39 }
44 40
@@ -54,20 +50,17 @@ func (this *UploadController) Voice() { @@ -54,20 +50,17 @@ 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 - } 55 + for _, v := range this.Ctx.Request.MultipartForm.File {
  56 + request.Files = v
65 if tmp, err = upload.Voice(request); err != nil { 57 if tmp, err = upload.Voice(request); err != nil {
66 log.Error(err) 58 log.Error(err)
67 return 59 return
68 } 60 }
69 rsp.Paths = append(rsp.Paths, tmp.Paths...) 61 rsp.Paths = append(rsp.Paths, tmp.Paths...)
70 } 62 }
  63 + }
71 msg = protocol.NewReturnResponse(rsp, err) 64 msg = protocol.NewReturnResponse(rsp, err)
72 } 65 }
73 66
@@ -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{} 78 var request = &protocol.FileRequest{}
86 - if request.Files, err = this.GetFiles("file"); err != nil { 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 {
87 log.Error(err) 84 log.Error(err)
88 return 85 return
89 } 86 }
90 - msg = protocol.NewReturnResponse(upload.Video(request)) 87 + rsp.Paths = append(rsp.Paths, tmp.Paths...)
  88 + }
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) 23 + //for i := range request.Files {
  24 + //f := request.Files[i]
  25 + //subfix := path.Ext(f.Filename)
26 //文件格式不符合 26 //文件格式不符合
27 - if !(subfix == ".jpg" || subfix == ".gif" || subfix == ".png") {  
28 - err = common.NewErrorWithMsg(2, "file format error")  
29 - return  
30 - }  
31 - } 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 }