add SaveToFile & docs
Showing
2 changed files
with
86 additions
and
0 deletions
| ... | @@ -6,10 +6,12 @@ import ( | ... | @@ -6,10 +6,12 @@ import ( |
| 6 | "encoding/xml" | 6 | "encoding/xml" |
| 7 | "github.com/astaxie/beego/session" | 7 | "github.com/astaxie/beego/session" |
| 8 | "html/template" | 8 | "html/template" |
| 9 | "io" | ||
| 9 | "io/ioutil" | 10 | "io/ioutil" |
| 10 | "mime/multipart" | 11 | "mime/multipart" |
| 11 | "net/http" | 12 | "net/http" |
| 12 | "net/url" | 13 | "net/url" |
| 14 | "os" | ||
| 13 | "path" | 15 | "path" |
| 14 | "strconv" | 16 | "strconv" |
| 15 | "strings" | 17 | "strings" |
| ... | @@ -197,6 +199,21 @@ func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, | ... | @@ -197,6 +199,21 @@ func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, |
| 197 | return c.Ctx.Request.FormFile(key) | 199 | return c.Ctx.Request.FormFile(key) |
| 198 | } | 200 | } |
| 199 | 201 | ||
| 202 | func (c *Controller) SaveToFile(fromfile, tofile string) error { | ||
| 203 | file, _, err := c.Ctx.Request.FormFile(fromfile) | ||
| 204 | if err != nil { | ||
| 205 | return err | ||
| 206 | } | ||
| 207 | defer file.Close() | ||
| 208 | f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) | ||
| 209 | if err != nil { | ||
| 210 | return err | ||
| 211 | } | ||
| 212 | defer f.Close() | ||
| 213 | io.Copy(f, file) | ||
| 214 | return nil | ||
| 215 | } | ||
| 216 | |||
| 200 | func (c *Controller) StartSession() (sess session.SessionStore) { | 217 | func (c *Controller) StartSession() (sess session.SessionStore) { |
| 201 | sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) | 218 | sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) |
| 202 | return | 219 | return | ... | ... |
| ... | @@ -237,10 +237,79 @@ beego采用了Go语言内置的模板引擎,所有模板的语法和Go的一 | ... | @@ -237,10 +237,79 @@ beego采用了Go语言内置的模板引擎,所有模板的语法和Go的一 |
| 237 | 也就是你对应的Controller名字+请求方法名.模板后缀,也就是如果你的Controller名是`AddController`,请求方法是`POST`,默认的文件后缀是`tpl`,那么就会默认请求`/viewpath/AddController/POST.tpl`文件。 | 237 | 也就是你对应的Controller名字+请求方法名.模板后缀,也就是如果你的Controller名是`AddController`,请求方法是`POST`,默认的文件后缀是`tpl`,那么就会默认请求`/viewpath/AddController/POST.tpl`文件。 |
| 238 | 238 | ||
| 239 | ### lauout设计 | 239 | ### lauout设计 |
| 240 | beego支持layout设计,例如你在管理系统中,其实整个的管理界面是固定的,支会变化中间的部分,那么你可以通过如下的设置: | ||
| 241 | |||
| 242 | this.Layout = "admin/layout.html" | ||
| 243 | this.TplNames = "admin/add.tpl" | ||
| 244 | |||
| 245 | 在layout.html中你必须设置如下的变量: | ||
| 246 | |||
| 247 | {{.LayoutContent}} | ||
| 248 | |||
| 249 | beego就会首先解析TplNames指定的文件,获取内容赋值给LayoutContent,然后最后渲染layout.html文件。 | ||
| 250 | |||
| 251 | 目前采用首先把目录下所有的文件进行缓存,所以用户还可以通过类似这样的方式实现layout: | ||
| 252 | |||
| 253 | {{template "header.html"}} | ||
| 254 | 处理逻辑 | ||
| 255 | {{template "footer.html"}} | ||
| 240 | 256 | ||
| 241 | ### 模板函数 | 257 | ### 模板函数 |
| 258 | beego支持用户定义模板函数,但是必须在`beego.Run()`调用之前,设置如下: | ||
| 259 | |||
| 260 | func hello(in string)(out string){ | ||
| 261 | out = in + "world" | ||
| 262 | return | ||
| 263 | } | ||
| 264 | |||
| 265 | beego.AddFuncMap("hi",hello) | ||
| 266 | |||
| 267 | 定义之后你就可以在模板中这样使用了: | ||
| 268 | |||
| 269 | {{.Content | hi}} | ||
| 270 | |||
| 271 | 目前beego内置的模板函数有如下: | ||
| 272 | |||
| 273 | * markdown | ||
| 274 | |||
| 275 | 实现了把markdown文本转化为html信息,使用方法{{markdown .Content}} | ||
| 276 | * dateformat | ||
| 277 | |||
| 278 | 实现了时间的格式化,返回字符串,使用方法{{dateformat .Time "2006-01-02T15:04:05Z07:00"}} | ||
| 279 | * date | ||
| 280 | |||
| 281 | 实现了类似PHP的date函数,可以很方便的根据字符串返回时间,使用方法{{date .T "Y-m-d H:i:s"}} | ||
| 282 | * compare | ||
| 283 | |||
| 284 | 实现了比较两个对象的比较,如果相同返回true,否者false,使用方法{{compare .A .B}} | ||
| 285 | * substr | ||
| 286 | |||
| 287 | 实现了字符串的截取,支持中文截取的完美截取,使用方法{{substr .Str 0 30}} | ||
| 288 | * html2str | ||
| 289 | |||
| 290 | 实现了把html转化为字符串,剔除一些script、css之类的元素,返回纯文本信息,使用方法{{html2str .Htmlinfo}} | ||
| 291 | * str2html | ||
| 292 | |||
| 293 | 实现了把相应的字符串当作HTML来输出,不转义,使用方法{{str2html .Strhtml}} | ||
| 294 | * htmlquote | ||
| 295 | |||
| 296 | 实现了基本的html字符转义,使用方法{{htmlquote .quote}} | ||
| 297 | * htmlunquote | ||
| 298 | |||
| 299 | 实现了基本的反转移字符,使用方法{{htmlunquote .unquote}} | ||
| 242 | 300 | ||
| 243 | ## request处理 | 301 | ## request处理 |
| 302 | 我们经常需要获取用户传递的数据,包括Get、POST等方式的请求,beego里面会自动解析这些数据,你可以通过如下方式获取数据 | ||
| 303 | |||
| 304 | - GetString | ||
| 305 | - GetInt | ||
| 306 | - GetBool | ||
| 307 | |||
| 308 | |||
| 309 | ### 文件上传 | ||
| 310 | |||
| 311 | - GetFile | ||
| 312 | - SaveToFile | ||
| 244 | 313 | ||
| 245 | ## 跳转和错误 | 314 | ## 跳转和错误 |
| 246 | 315 | ... | ... |
-
Please register or sign in to post a comment