우리를 구현하기 위해 노력하고 있습니다 이미지 업로드를 통해 POST 요청합니다. 우리는 또한 제한하려면 이미지를~1,0MiB. 그것은 잘 작동에 작은 이미지를,아무것도하지만~2,5MiB 또는 더 큰 원인 연결을 재설정합니다. 그것은 또한 것을 여러 요청을 보내 후 첫 번째 동일한 처리기입니다.
습니다.이동:
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", uploadHandler)
http.ListenAndServe("localhost:8080", nil)
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
postHandler(w, r)
return
} else {
http.ServeFile(w, r, "index.html")
}
}
func postHandler(w http.ResponseWriter, r *http.Request) {
// Send an error if the request is larger than 1 MiB
if r.ContentLength > 1<<20 {
// if larger than ~2,5 MiB, this will print 2 or more times
log.Println("File too large")
// And this error will never arrive, instead a Connection reset
http.Error(w, "response too large", http.StatusRequestEntityTooLarge)
return
}
return
}
index.html:
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="file" accept="image/*" name="profile-picture"><br>
<button type="submit" >Upload</button>
</form>
</body>
</html>
출력 업로드할 때~2,4MiB 파일
$ go run main.go
2021/11/23 22:00:14 File too large
그것은 또한 요청"너무 큰에서"브라우저
출력 업로드할 때~2,5MiB 파일
$ go run main.go
2021/11/23 22:03:25 File too large
2021/11/23 22:03:25 File too large
브라우저 지금 보는 연결을 다시 설정
20 << 20
어떤 이유에서 나는 할 수 없을 것이 아무것도 업로드의 크기입니다.