routers->adminRouters.go->
package routers
import (
"gindemo10/controllers/admin"
"gindemo10/middlewares"
"github.com/gin-gonic/gin"
)
func AdminRoutersInit(r *gin.Engine) {
//middlewares.InitMiddleware中间件
adminRouters := r.Group("/admin", middlewares.InitMiddleware)
{
adminRouters.GET("/", admin.IndexController{}.Index)
adminRouters.GET("/user", admin.UserController{}.Index)
adminRouters.GET("/user/add", admin.UserController{}.Add)
adminRouters.POST("/user/doUpload", admin.UserController{}.DoUpload)
adminRouters.GET("/user/edit", admin.UserController{}.Edit)
adminRouters.POST("/user/doEdit", admin.UserController{}.DoEdit)
adminRouters.GET("/article", admin.ArticleController{}.Index)
adminRouters.GET("/article/add", admin.ArticleController{}.Add)
adminRouters.GET("/article/edit", admin.ArticleController{}.Edit)
}
}
controllers->admin->userController.go
package admin
import (
"net/http"
"path"
"github.com/gin-gonic/gin"
)
type UserController struct {
BaseController
}
func (con UserController) Index(c *gin.Context) {
c.String(200, "用户列表--")
// con.success(c)
}
func (con UserController) Add(c *gin.Context) {
c.HTML(http.StatusOK, "admin/useradd.html", gin.H{})
}
func (con UserController) DoUpload(c *gin.Context) {
username := c.PostForm("username")
file, err := c.FormFile("face")
// file.Filename 获取文件名称 aaa.jpg ./static/upload/aaa.jpg
dst := path.Join("./static/upload", file.Filename)
if err == nil {
c.SaveUploadedFile(file, dst)
}
// c.String(200, "执行上传")
c.JSON(http.StatusOK, gin.H{
"success": true,
"username": username,
"dst": dst,
})
}
func (con UserController) Edit(c *gin.Context) {
c.HTML(http.StatusOK, "admin/useredit.html", gin.H{})
}
func (con UserController) DoEdit(c *gin.Context) {
username := c.PostForm("username")
face1, err1 := c.FormFile("face1")
dst1 := path.Join("./static/upload", face1.Filename)
if err1 == nil {
c.SaveUploadedFile(face1, dst1)
}
face2, err2 := c.FormFile("face2")
dst2 := path.Join("./static/upload", face2.Filename)
if err2 == nil {
c.SaveUploadedFile(face2, dst2)
}
// c.String(200, "执行上传")
c.JSON(http.StatusOK, gin.H{
"success": true,
"username": username,
"dst1": dst1,
"dst2": dst2,
})
}
{{ define "admin/useradd.html" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>演示文件上传</h2>
<form action="/admin/user/doUpload" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username" placeholder="用户名" />
<br>
<br>
头 像<input type="file" name="face" />
<br> <br>
<input type="submit" value="提交">
</form>
</body>
</html>
{{ end }}
浙公网安备 33010602011771号