Ejemplo n.º 1
0
//上传图片
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()
}
Ejemplo n.º 2
0
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()
}
Ejemplo n.º 3
0
/* 根据条件保存上传文件 */
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() //返回文件路径、大小
}
Ejemplo n.º 4
0
//上传图片
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()
}