我运行关于文件上传的 gin 示例,这个 repo 来自https://github.com/gin-gonic/examples/tree/5898505356e9064c49abb075eae89596a3c5cd67/upload-file/single。当我改变是极限
router.MaxMultipartMemory = 1 // 8 MiB
但没有为上传大文件而醒来,任何人都知道这一点。
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 1 // 8 MiB
fmt.Println(router.MaxMultipartMemory)
router.Static("/", "./public")
router.POST("/upload", func(c *gin.Context) {
ct := c.Request.Header.Get("Content-Type")
fmt.Println(ct)
name := c.PostForm("name")
email := c.PostForm("email")
// Source
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
if err := c.SaveUploadedFile(file, file.Filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email))
})
router.Run(":8080")
}
我希望当文件大小大于限制时,应该会出现错误。
更新
误会,MaxMultipartMemory只是限制内存,不是针对文件上传文件大小,即使文件大小比这个大,也会写入临时文件。
慕运维8079593
沧海一幻觉
相关分类