package Api import ( "fmt" "github.com/aarongao/tools" "github.com/gin-gonic/gin" "path" "strconv" "time" ) // @Title 上传文件 // @Description 上传 // @Accept json // @Produce json // @Param file 1 file true "文件" // @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"图片地址"}" // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" // @Router /Upload? [post] func Upload(c *gin.Context) { c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) c.Header("Access-Control-Allow-Credentials", "true") file, err := c.FormFile("file") if err != nil { c.JSON(200, tools.ResponseError{ 1, "a Bad request", }) return } fileName := file.Filename fileExt := path.Ext(fileName) filePath := "Upload/" + strconv.Itoa(int(time.Now().UnixNano())) + fileExt if err := c.SaveUploadedFile(file, filePath); err != nil { fmt.Println(err) c.JSON(200, tools.ResponseError{ 1, "upload file err", }) return } c.JSON(200, tools.ResponseSeccess{ 0, "/" + filePath, }) }