一、使用Postman中form-data方式:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func upload360(c *gin.Context) {
bit := c.PostForm("bit")
fmt.Println(bit)
file,err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest,gin.H{
"message":"a Bad request!",
})
return
}
nowTime := string([]rune(time.Now().String())[0:10])
if err := c.SaveUploadedFile(file,"F:\\Upload360\\360Update"+nowTime+"_"+bit+".exe"); err != nil {
c.JSON(http.StatusOK,gin.H{
"message":err,
})
return
}
c.JSON(http.StatusOK,gin.H{
"message":"upload successful",
})
}
func main() {
router := gin.Default()
router.POST("/upload",upload360)
router.Run(":8899")
}
二、使用Postman中binary或者form-data方式:
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
nowTime := string([]rune(time.Now().String())[0:10])
file, err := os.Create("F:\\Upload360\\360Update"+nowTime+".exe")
if err != nil {
panic(err)
}
_, err = io.Copy(file, r.Body)
if err != nil {
panic(err)
}
w.Write([]byte("upload success"))
}
func main() {
fmt.Println("Start...")
http.HandleFunc("/upload", uploadHandler)
http.ListenAndServe(":8899", nil)
}
有疑问加站长微信联系(非本文作者)