1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-02 09:33:27 +00:00

add SaveToFile & docs

This commit is contained in:
astaxie 2013-04-16 18:20:55 +08:00
parent e865325556
commit 6ce02f1d1c
2 changed files with 347 additions and 261 deletions

View File

@ -6,10 +6,12 @@ import (
"encoding/xml"
"github.com/astaxie/beego/session"
"html/template"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
@ -197,6 +199,21 @@ func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader,
return c.Ctx.Request.FormFile(key)
}
func (c *Controller) SaveToFile(fromfile, tofile string) error {
file, _, err := c.Ctx.Request.FormFile(fromfile)
if err != nil {
return err
}
defer file.Close()
f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer f.Close()
io.Copy(f, file)
return nil
}
func (c *Controller) StartSession() (sess session.SessionStore) {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
return

View File

@ -237,10 +237,79 @@ beego采用了Go语言内置的模板引擎所有模板的语法和Go的一
也就是你对应的Controller名字+请求方法名.模板后缀也就是如果你的Controller名是`AddController`,请求方法是`POST`,默认的文件后缀是`tpl`,那么就会默认请求`/viewpath/AddController/POST.tpl`文件。
### lauout设计
beego支持layout设计例如你在管理系统中其实整个的管理界面是固定的支会变化中间的部分那么你可以通过如下的设置
this.Layout = "admin/layout.html"
this.TplNames = "admin/add.tpl"
在layout.html中你必须设置如下的变量
{{.LayoutContent}}
beego就会首先解析TplNames指定的文件获取内容赋值给LayoutContent然后最后渲染layout.html文件。
目前采用首先把目录下所有的文件进行缓存所以用户还可以通过类似这样的方式实现layout
{{template "header.html"}}
处理逻辑
{{template "footer.html"}}
### 模板函数
beego支持用户定义模板函数但是必须在`beego.Run()`调用之前,设置如下:
func hello(in string)(out string){
out = in + "world"
return
}
beego.AddFuncMap("hi",hello)
定义之后你就可以在模板中这样使用了:
{{.Content | hi}}
目前beego内置的模板函数有如下
* markdown
实现了把markdown文本转化为html信息使用方法{{markdown .Content}}
* dateformat
实现了时间的格式化,返回字符串,使用方法{{dateformat .Time "2006-01-02T15:04:05Z07:00"}}
* date
实现了类似PHP的date函数可以很方便的根据字符串返回时间使用方法{{date .T "Y-m-d H:i:s"}}
* compare
实现了比较两个对象的比较如果相同返回true否者false使用方法{{compare .A .B}}
* substr
实现了字符串的截取,支持中文截取的完美截取,使用方法{{substr .Str 0 30}}
* html2str
实现了把html转化为字符串剔除一些script、css之类的元素返回纯文本信息使用方法{{html2str .Htmlinfo}}
* str2html
实现了把相应的字符串当作HTML来输出不转义使用方法{{str2html .Strhtml}}
* htmlquote
实现了基本的html字符转义使用方法{{htmlquote .quote}}
* htmlunquote
实现了基本的反转移字符,使用方法{{htmlunquote .unquote}}
## request处理
我们经常需要获取用户传递的数据包括Get、POST等方式的请求beego里面会自动解析这些数据你可以通过如下方式获取数据
- GetString
- GetInt
- GetBool
### 文件上传
- GetFile
- SaveToFile
## 跳转和错误