完成图片下载接口

master
a74589669 2019-09-03 00:22:39 +08:00
parent 1cab2b6130
commit 3509535dae
2 changed files with 21 additions and 13 deletions

View File

@ -1,10 +1,8 @@
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
"io"
"io/ioutil"
"log"
"net/http"
@ -51,16 +49,26 @@ func (this *FileController) OnUpload(c *gin.Context) {
}
func (this *FileController) OnDownLoad(c *gin.Context) {
file, header, err := c.Request.FormFile("upload")
filename := header.Filename
fmt.Println(header.Filename)
out, err := os.Create("G://GoPath//image//" + filename + ".png")
if err != nil {
log.Fatal(err)
resp := RespBase{Msg:"FAIL",Status: 211}
defer func() {
c.JSON(200,resp)
}()
fileName := c.Param("file")
if "" == fileName{
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
log.Fatal(err)
file,e := os.Open("G://image//" +fileName)
if nil != e{
log.Print(e.Error())
return
}
bytes,e :=ioutil.ReadAll(file)
if nil != e{
log.Print(e.Error())
return
}
c.Header("Content-Type","image/png")
c.Header("Content-Disposition",
"G://image//" +fileName)
c.Writer.Write(bytes)
}

View File

@ -100,7 +100,7 @@ func main() {
api.POST("/article_update",controller.UpdateArtilce)
api.POST("/image_upload",fileController.OnUpload)
api.GET("/image_download/:file",fileController.OnDownLoad)
}
e := r.Run(":" + strconv.Itoa(config.GetPort()))