//上传
func PostFile(c *gin.Context) {
file, err := c.FormFile("file")
if err ==nil {
Path := "./public/image/"
t := time.Now()
date := t.Format("20060102")
pathTmp := Path + "/ " + date +"/"
if isDirExists(pathTmp) {
fmt.Println("目录存在")
} else {
fmt.Println("目录不存在")
err := os.Mkdir(pathTmp, 0777)
if err != nil {
//log.Fatal(err)
c.JSON(200, gin.H{"status": -1, "msg": "创建目录失败",})
}
}
//文件名
file_name := strconv.FormatInt(time.Now().Unix(),10) + strconv.Itoa(rand.Intn(999999-100000)+100000) + path.Ext(file.Filename)
uperr := c.SaveUploadedFile(file, pathTmp + file_name)
if(uperr==nil) {
c.JSON(200, gin.H{"status": 1, "msg": "上传成功","data":date+"/"+file_name})
} else {
c.JSON(200, gin.H{"status": -2, "msg": "上传失败",})
}
//c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
} else {
c.JSON(200, gin.H{"status": -1, "msg": "上传失败",})
}
}
//目录是否存在
func isDirExists(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}