1、上传单个文
views
beego 中 上 传 图 片 是 非 常 简 单 的 , 但 是 要 注 意 的 是 form 表 单 中 必 须 加 入 enctype="multipart/form-data"这个属性,否则你的浏览器不会传输你上传的文件。
article.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Article</title> </head> <body> <h2>上传文章</h2> <form method="post" action="/article/doUpload" enctype="multipart/form-data"> 标题: <input type="text" name="title" /> <br><br> 内容: <input type="text" name="content" /> <br><br> 文件:<input type="file" name="pic" /> <br><br> <input type="submit" value="提交"> </form> </body> </html>
routers
package routers import ( "beegoupload/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/focus", &controllers.FocusController{}) beego.Router("/focus/doUpload", &controllers.FocusController{}, "post:DoUpload") beego.Router("/article", &controllers.ArticleController{}) beego.Router("/article/doUpload", &controllers.ArticleController{}, "post:DoUpload") }
controllers
GetFile 可以接受传过来的图片和文件
package controllers import ( "github.com/astaxie/beego" ) type ArticleController struct { beego.Controller } func (c *ArticleController) Get() { c.TplName = "article.html" } func (c *ArticleController) DoUpload() { title := c.GetString("title") content := c.GetString("content") beego.Info(title) beego.Info(content) //执行上传文件 f, h, err := c.GetFile("pic") if err != nil { beego.Error(err) c.Ctx.WriteString("上传文件失败") return } c.SaveToFile("pic", "static/upload/"+h.Filename) //目录必须提前创建好 //关闭文件流 defer f.Close() c.Ctx.WriteString("上传文件成功") }
2、上传多个文件
focus.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Focus</title> </head> <body> <h2>上传轮播图</h2> <form method="post" action="/focus/doUpload" enctype="multipart/form-data"> 标题: <input type="text" name="title" /> <br><br> 文件1:<input type="file" name="pic1" /><br><br> 文件2:<input type="file" name="pic2" /> <br><br> <input type="submit" value="提交"> </form> </body> </html>
package controllers import ( "github.com/astaxie/beego" ) type FocusController struct { beego.Controller } func (c *FocusController) Get() { c.TplName = "focus.html" } func (c *FocusController) DoUpload() { title := c.GetString("title") beego.Info(title) //接收第一个文件 f1, h1, err1 := c.GetFile("pic1") if err1 != nil { beego.Warning(err1) } else { //关闭文件流 defer f1.Close() c.SaveToFile("pic1", "static/upload/"+h1.Filename) } //接收第二个文件 f2, h2, err2 := c.GetFile("pic2") if err2 != nil { beego.Warning(err2) } else { defer f2.Close() c.SaveToFile("pic2", "static/upload/"+h2.Filename) } c.Ctx.WriteString("上传文件成功") }
3、上传类型判断、以及根据日期生成存储目录
views
goods.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>商品上传</title> </head> <body> <h2>上传商品</h2> <form method="post" action="/goods/doUpload" enctype="multipart/form-data"> 标题: <input type="text" name="title" /> <br><br> 文件:<input type="file" name="pic" /> <br><br> <input type="submit" value="提交"> </form> </body> </html>
routers
package routers import ( "beegoupload/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/focus", &controllers.FocusController{}) beego.Router("/focus/doUpload", &controllers.FocusController{}, "post:DoUpload") beego.Router("/article", &controllers.ArticleController{}) beego.Router("/article/doUpload", &controllers.ArticleController{}, "post:DoUpload") beego.Router("/goods", &controllers.GoodsController{}) beego.Router("/goods/doUpload", &controllers.GoodsController{}, "post:DoUpload") }
controllers
package controllers import ( "beegoupload/models" "os" "path" "strconv" "github.com/astaxie/beego" ) type GoodsController struct { beego.Controller } func (c *GoodsController) Get() { c.TplName = "goods.html" } func (c *GoodsController) DoUpload() { title := c.GetString("title") beego.Info(title) //1、获取上传的文件 f, h, err := c.GetFile("pic") if err != nil { beego.Error(err) return } //2、关闭文件流 defer f.Close() //3、获取后缀名 判断类型是否正确 .jpg .png .gif .jpeg extName := path.Ext(h.Filename) allowExtMap := map[string]bool{ ".jpg": true, ".png": true, ".gif": true, ".jpeg": true, } if _, ok := allowExtMap[extName]; !ok { c.Ctx.WriteString("文件类型不合法") return } //4、创建图片保存目录 static/upload/20200623 day := models.GetDay() dir := "static/upload/" + day if err := os.MkdirAll(dir, 0666); err != nil { beego.Error(err) } //5、生成文件名称 144325235235.png fileUnixName := strconv.FormatInt(models.GetUnix(), 10) //static/upload/20200623/144325235235.png saveDir := path.Join(dir, fileUnixName+extName) //6、保存图片 c.SaveToFile("pic", saveDir) c.Ctx.WriteString("上传图片成功") }
models
package models import ( "crypto/md5" "fmt" "time" "github.com/astaxie/beego" ) //时间戳间戳转换成日期 func UnixToDate(timestamp int) string { t := time.Unix(int64(timestamp), 0) return t.Format("2006-01-02 15:04:05") } //日期转换成时间戳 2020-05-02 15:04:05 func DateToUnix(str string) int64 { template := "2006-01-02 15:04:05" t, err := time.ParseInLocation(template, str, time.Local) if err != nil { beego.Info(err) return 0 } return t.Unix() } func GetUnix() int64 { return time.Now().Unix() } func GetDate() string { template := "2006-01-02 15:04:05" return time.Now().Format(template) } func GetDay() string { template := "20060102" return time.Now().Format(template) } func Md5(str string) string { data := []byte(str) return fmt.Sprintf("%x ", md5.Sum(data)) } func Hello(in string) (out string) { out = in + "world" return }