เรากำลังพยายามจะดำเนิกำลังอัพโหลดภาพผ่านโพสต์สิ่งที่ร้องขอมา เรายังต้องการเพื่อจำกัดภาพที่จะ~1,0 เมกะไบต์. มันทำงานได้ดีขึ้นเล็กลงภาพแต่อะไร~2,5 เมกะไบต์หรือใหญ่เป็นโตยอมรับเถอะแอนการเชื่อมต่อสำหรับปรับค่า. มันดูเหมือนจะส่งหลายขอร้องให้หลังจากคนแรกที่เดียวกับเครื่องมือจัดการ.
หลัก.ไป:
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,4 เมกะไบต์แฟ้ม
$ go run main.go
2021/11/23 22:00:14 File too large
มันก็ยังแสดงถึง"คำร้องขอขนาดใหญ่เกินไป"ในเบราว์เซอร์
ส่งออกตอนที่กำลังอัพโหลด~2,5 เมกะไบต์แฟ้ม
$ go run main.go
2021/11/23 22:03:25 File too large
2021/11/23 22:03:25 File too large
เบราว์เซอร์ตอนนี้แสดงว่าการเชื่อมต่อถูกปรับค่า
20 << 20
ด้วยเหตุผลอะไรก็ตามสิ่งที่ฉันไม่สามารถอัพโหลดอะไรของตัวขนาดนั้น