定义模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="f1">
<input type="submit" value="上传">
</form>
</body>
</html>
解析模板
r := gin.Default()
r.LoadHTMLFiles("src/gin_test/index.html")
r.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK,"index.html",nil)
})
等上传好图片之后,就会执行upload方法,然后upload方法对上传好的图片进行一系列操作
r.POST("/upload", func(c *gin.Context) {
//从请求中读取文件
f, err := c.FormFile("f1")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H {
"error": err.Error(),
})
}else {
//dst := fmt.Sprintf("./%s", f.Filename)
dst := path.Join("./", f.Filename)
//将文件保存在指定目录中
c.SaveUploadedFile(f,dst)
c.JSON(http.StatusOK, gin.H {
"status": "ok",
})
}
//将读取到的文件保存在本地
})
r.Run(":9090")