今天用beego做了一个实现自动进行数据库备份,每天将数据库发送到其他电脑上进行备份。
发送文件的
func Postdata() {
b := httplib.Post("http://127.0.0.1:80/postdata")
b.Param("username", "astaxie")
b.Param("password", "123456")
b.PostFile("uploadfile1", "d:\\1.txt")
// b.PostFile("uploadfile2", "httplib.txt")PostFile 第一个参数是 form 表单的字段名,第二个是需要发送的文件名或者文件路径
str, err := b.String()
if err != nil {
beego.Error(str)
}
}
接受文件的:
func (c *MainController) Postdata() {
f, h, err := c.GetFile("uploadfile1")
// beego.Info(h)//这里 filename是路径,所以不能以filename作为保存的文件名。坑!!
defer f.Close()
if err != nil {
beego.Error(err)
} else {
c.SaveToFile("uploadfile1", "./static/upload/1.txt") // 保存位置在 static/upload, 没有文件夹要先创建
c.Ctx.WriteString("ok")
}
}
自动定时任务