完成图片下载接口

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 package controller
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -51,16 +49,26 @@ func (this *FileController) OnUpload(c *gin.Context) {
} }
func (this *FileController) OnDownLoad(c *gin.Context) { func (this *FileController) OnDownLoad(c *gin.Context) {
file, header, err := c.Request.FormFile("upload") resp := RespBase{Msg:"FAIL",Status: 211}
filename := header.Filename defer func() {
fmt.Println(header.Filename) c.JSON(200,resp)
out, err := os.Create("G://GoPath//image//" + filename + ".png") }()
if err != nil { fileName := c.Param("file")
log.Fatal(err) if "" == fileName{
return
} }
defer out.Close() file,e := os.Open("G://image//" +fileName)
_, err = io.Copy(out, file) if nil != e{
if err != nil { log.Print(e.Error())
log.Fatal(err) 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("/article_update",controller.UpdateArtilce)
api.POST("/image_upload",fileController.OnUpload) api.POST("/image_upload",fileController.OnUpload)
api.GET("/image_download/:file",fileController.OnDownLoad)
} }
e := r.Run(":" + strconv.Itoa(config.GetPort())) e := r.Run(":" + strconv.Itoa(config.GetPort()))