golang 文件上传和下载以及swagger配置
上传:
// @Summary 上传文件 // @Description // @Tags file // @Accept multipart/form-data // @Param file formData file true "file" // @Produce json // @Success 200 {object} filters.Response {"code":200,"data":nil,"msg":""} // @Router /upload [post] func UploadFile(ctx *gin.Context) { file, header, err := ctx.Request.FormFile("file") if err != nil { returnMsg(ctx, configs.ERROR_PARAMS, "", err.Error()) return } //获取文件名 filename := header.Filename //写入文件 out, err := os.Create("./static/" + filename) if err != nil { returnMsg(ctx, configs.ERROR_SERVERE, "", err.Error()) return } defer out.Close() _, err = io.Copy(out, file) if err != nil { log.Fatal(err) } returnMsg(ctx, 200, "", "success") }
下载:
// @Summary 下载文件 // @Description // @Tags file // @Param filename query string true "file name" // @Success 200 {object} gin.Context // @Router /download [get] func DownloadFile(ctx *gin.Context) { filename := ctx.DefaultQuery("filename", "") //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名 ctx.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename)) ctx.Writer.Header().Add("Content-Type", "application/octet-stream") ctx.File("./static/a.txt") }
上传下载读取csv等文件: https://blog.csdn.net/aaaadong/article/details/90177038
相关推荐
GoatSucker 2020-11-23
CoderToy 2020-11-16
蛰脚踝的天蝎 2020-11-10
ZGCdemo 2020-11-09
86193952 2020-10-27
Lzs 2020-10-23
hjr 2020-10-21
HELLOBOY 2020-10-21
qidiantianxia 2020-10-21
cmsmdn 2020-10-21
myspace 2020-10-21
笑面依旧 2020-10-21
专注前端开发 2020-10-21
技术之博大精深 2020-10-16
zhushenghan 2020-11-09
风雨断肠人 2020-10-13
xuguiyi00 2020-11-04
sichenglain 2020-10-27