<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/filed">
<input type="file" name="filename" />
<input type="submit">
</form>
</body>
</html>
package routers import ( "booktest/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/files",&controllers.BookController{},"get:FileS") beego.Router("/filed",&controllers.BookController{},"post:FileD") }
func (b *BookController) FileS() { b.TplName="filed.html" } func (this *BookController) FileD() { // 读取文件信息 f, h, err := this.GetFile("filename") if err != nil { log.Fatal("读取文件错误", err) } // 延迟关闭文件 defer f.Close() //查看文件可忽略 可保存文件以后在读取 data, err := ioutil.ReadAll(f) if err != nil { logs.Debug(err) } this.Ctx.WriteString(string(data)) // 保存文件, 本地文件路径static/upload/上传文件名 // 需要提前创建好static/upload目录 this.SaveToFile("filename", "static/upload/" + h.Filename) }
原文:https://www.cnblogs.com/itcjl/p/13552992.html