我们正在努力实施的图像上传通过后请求。 我们还希望限制的图像到-1.0的信息库。 它的工作现在较小的图像,但任何东西~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>
输出当载a-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
无论出于何种原因我不能上任何东西的大小。