完成图片下载接口
parent
1cab2b6130
commit
3509535dae
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -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()))
|
||||||
|
|
Loading…
Reference in New Issue