gin上传文件服务器,使用Gin框架上传文件到本地

func UpLoadFileToLocal(c *gin.Context) {

_,header,err :=c.Request.FormFile("file")

tools.HasError(err,"",-1)

// 限制文件上传大小

if float64(header.Size)/1024/1024 >10{

tools.HasError(errors.New(""),"上传文件不能大于10M",-1)

}

nameArr :=strings.Split(header.Filename,".")

suffixName :=nameArr[len(nameArr)-1]

// 验证 是否支持该格式上传,懒人写法

suffixArr := []string{"xlsx","txt","jpg","png","jpeg","doc","docx","ppt","pptx","xls","mp3","mp4","exe"}

var indexStr string

for i :=0; i

if suffixArr[i] ==suffixName{

indexStr ="."+suffixArr[i]

}

}

if indexStr ==""{

tools.HasError(errors.New(""),"暂时不支持该格式上传",-1)

}

err =c.SaveUploadedFile(header,"./temp/file/"+nameArr[0]+indexStr)

tools.HasError(err,"保存本地失败",-1)

app.OK(c,nil,"")

}

你可能感兴趣的:(gin上传文件服务器)