1 个回答
在Gin框架中,可以使用MultipartForm函数来处理文件上传。MultipartForm函数会自动解析multipart/form-data格式的表单数据,并将文件保存在指定的目录中。例如:
func UploadFile(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, filename); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"filename": filename})
}
发布于:1个月前 (02-12) IP属地:四川省
我来回答
您需要 登录 后回答此问题!