本文整理汇总了Golang中github.com/astaxie/beego.Controller.GetFile方法的典型用法代码示例。如果您正苦于以下问题:Golang Controller.GetFile方法的具体用法?Golang Controller.GetFile怎么用?Golang Controller.GetFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/astaxie/beego.Controller
的用法示例。
在下文中一共展示了Controller.GetFile方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: WebUploadImage
//上传图片
func WebUploadImage(this *beego.Controller) {
//filename := this.Input().Get("Filename")
f, h, _ := this.GetFile("Filedata")
filename := h.Filename
f.Close() //关闭,减少缓存
ext := filename[strings.LastIndex(filename, ".")+1:]
//获取扩展名
if !strings.Contains(allowImageType, ext) {
fmt.Println(filename)
this.Ctx.WriteString("{\"state\":\"0\"}")
this.StopRun()
}
newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename
err := this.SaveToFile("Filedata", uploadimage+newname)
state := "SUCCESS"
if err != nil {
fmt.Println(err)
state = "0"
}
state = "1"
url := website + uploadimage + newname
//this.Ctx.WriteString("{'original':'" + filename + "','url':'" + url + "','title':'" + this.Input().Get("pictitle") + "','state':'" + state + "'}")
//this.Ctx.WriteString("{\"state\": \"" + state + "\", \"url\": \"" + url + "\", \"title\": \"\",\"original\": \"" + filename + "\"}")
this.Ctx.WriteString("{\"status\": " + state + ", \"msg\": \"上传文件成功!\", \"name\": \"1.jpg\", \"path\": \"" + url + "\", \"thumb\": \"" + url + "\", \"size\": 0, \"ext\": \"" + ext + "\"}")
this.StopRun()
}
示例2: UploadFile
func UploadFile(this *beego.Controller) {
//filename := this.Input().Get("Filename")
f, h, _ := this.GetFile("upfile")
filename := h.Filename
f.Close() //关闭,减少缓存
index := strings.LastIndex(filename, ".")
filetype := ""
if index == -1 {
this.Ctx.WriteString("{\"state\":\"FAILED\"}")
this.StopRun()
}
filetype = filename[index:]
ext := filetype[1:]
if !strings.Contains(allowFileType, ext) {
this.Ctx.WriteString("{\"state\":\"FAILED\"}")
this.StopRun()
}
newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename
err := this.SaveToFile("upfile", uploadfile+newname)
state := "SUCCESS"
if err != nil {
fmt.Println(err)
state = "FAILED"
}
url := website + uploadfile + newname
this.Ctx.WriteString("{\"state\": \"" + state + "\", \"url\": \"" + url + "\", \"title\": \"\",\"original\": \"" + filename + "\"}")
this.StopRun()
}
示例3: SaveFile
/* 根据条件保存上传文件 */
func (this *ApiController) SaveFile(contro *beego.Controller, size int64, suf []string) (bool, string, int64) {
//获取文件信息
f, h, err := contro.GetFile("file")
if err != nil {
UserLog.Error("文件上传失败:", err)
f.Close()
return false, "", 0
}
f.Close()
//保存文件到临时目录
contro.SaveToFile("file", "static/tmp/"+h.Filename)
// 打开文件
fh, err := os.Open("static/tmp/" + h.Filename)
if err != nil {
return false, "", 0
}
//获取文件具体信息
stat, _ := fh.Stat()
if stat.Size() > size { //文件大小不合格,删除文件
//关闭文件
fh.Close()
os.Remove("static/tmp/" + h.Filename)
return false, "", 0
}
nameSplit := strings.Split(h.Filename, ".")
suffix := nameSplit[len(nameSplit)-1]
ok := false
//后缀不合格则删除文件
for _, v := range suf {
if suffix == v { //符合其中一个后缀
ok = true
}
}
if !ok {
//关闭文件
fh.Close()
os.Remove("static/tmp/" + h.Filename)
return false, "", 0
}
//关闭文件
fh.Close()
return true, "static/tmp/" + h.Filename, stat.Size() //返回文件路径、大小
}
示例4: UploadImage
//上传图片
func UploadImage(this *beego.Controller) {
f, h, _ := this.GetFile("upfile")
filename := h.Filename
f.Close() //关闭,减少缓存
//获取扩展名
ext := filename[strings.LastIndex(filename, ".")+1:]
if !strings.Contains(allowImageType, ext) {
fmt.Println(filename)
this.Ctx.WriteString("{\"state\":\"FAILED\"}")
this.StopRun()
}
newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename
err := this.SaveToFile("upfile", uploadimage+newname)
state := "SUCCESS"
if err != nil {
fmt.Println(err)
state = "FAILED"
}
url := website + uploadimage + newname
this.Ctx.WriteString("{\"state\": \"" + state + "\", \"url\": \"" + url + "\", \"title\": \"\",\"original\": \"" + filename + "\"}")
this.StopRun()
}