|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
"io"
|
|
|
"mime/multipart"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/protocol"
|
|
|
"os"
|
|
|
)
|
|
|
|
|
|
//UploadController 文件上传
|
|
|
type UploadController struct {
|
|
|
BaseController
|
|
|
}
|
|
|
|
|
|
//UploadImage 图片上传
|
|
|
//@router /image
|
|
|
func (c *UploadController) UploadImage() {
|
|
|
var msg *protocol.ResponseMessage
|
|
|
defer func() {
|
|
|
c.ResposeJson(msg)
|
|
|
}()
|
|
|
filehead, err := c.GetFiles("file")
|
|
|
if err != nil {
|
|
|
log.Error("获取上传文件失败:%s", err)
|
|
|
}
|
|
|
for i := range filehead {
|
|
|
err := saveFile(filehead[i], "")
|
|
|
if err != nil {
|
|
|
msg = protocol.BadRequestParam("1")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
msg = protocol.NewReturnResponse(nil, nil)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func saveFile(fileData *multipart.FileHeader, src string) error {
|
|
|
os.MkdirAll("./file/opp", 0777)
|
|
|
file, err := fileData.Open()
|
|
|
defer file.Close()
|
|
|
if err != nil {
|
|
|
log.Error("文件获取失败%s", err)
|
|
|
return err
|
|
|
}
|
|
|
dst, err := os.Create("./file/opp/" + fileData.Filename)
|
|
|
defer dst.Close()
|
|
|
if err != nil {
|
|
|
log.Error("目录访问失败:%s", err)
|
|
|
|
|
|
return err
|
|
|
}
|
|
|
if _, err := io.Copy(dst, file); err != nil {
|
|
|
log.Error("文件保存失败:%s", err)
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
} |
...
|
...
|
|