From 3509535daeaaf61d4e2f8546de184333ee3aa3e7 Mon Sep 17 00:00:00 2001 From: a74589669 <290198252@qq.com> Date: Tue, 3 Sep 2019 00:22:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9B=BE=E7=89=87=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/file.go | 32 ++++++++++++++++++++------------ main.go | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/controller/file.go b/controller/file.go index 7ed601b..82766eb 100644 --- a/controller/file.go +++ b/controller/file.go @@ -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) } diff --git a/main.go b/main.go index d7a0fe3..f7e5e38 100644 --- a/main.go +++ b/main.go @@ -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()))