단일 파일 업로드
const maxSize = 8 * iris.MB
func main() {
app := iris.Default()
app.Post("/upload", func(ctx iris.Context) {
// 요청 본문 크기 제한 설정, 파일 업로드 크기를 제한합니다 (기본값은 32 MiB)
ctx.SetMaxRequestBodySize(maxSize)
// 또는
// app.Use(iris.LimitRequestBodySize(maxSize))
// 또는
// iris.WithPostMaxMemory(maxSize)
// 파일 읽기
file, fileHeader, err:= ctx.FormFile("file")
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
// 업로드된 파일을 지정된 디렉터리에 저장
dest := filepath.Join("./uploads", fileHeader.Filename)
ctx.SaveFormFile(fileHeader, dest)
ctx.Writef("파일: %s 업로드 완료!", fileHeader.Filename)
})
app.Listen(":8080")
}
파일 업로드 테스트
curl -X POST http://localhost:8080/upload \
-F "file=@/Users/kataras/test.zip" \
-H "Content-Type: multipart/form-data"
다중 파일 업로드
func main() {
app := iris.Default()
app.Post("/upload", func(ctx iris.Context) {
// 다중 파일 읽기
files, n, err := ctx.UploadFormFiles("./uploads")
if err != nil {
ctx.StopWithStatus(iris.StatusInternalServerError)
return
}
ctx.Writef("%d개의 파일이 총 크기 %d로 업로드 완료!", len(files), n)
})
app.Listen(":8080", iris.WithPostMaxMemory(8 * iris.MB))
}
다중 파일 업로드 테스트
curl -X POST http://localhost:8080/upload \
-F "upload[]=@/Users/kataras/test1.zip" \
-F "upload[]=@/Users/kataras/test2.zip" \
-H "Content-Type: multipart/form-data"