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