1 回答

TA貢獻1783條經驗 獲得超4個贊
客戶端正在嘗試向服務器發送數據。服務器沒有讀取數據,它只是查看標題并關閉連接??蛻舳藢⒋私忉尀椤斑B接已重置”。這是你無法控制的。
不是檢查標題,而是標題可以撒謊,用于http.MaxBytesReader
讀取實際內容,但如果它太大,則會出錯。
const MAX_UPLOAD_SIZE = 1<<20
func postHandler(w http.ResponseWriter, r *http.Request) {
// Wrap the body in a reader that will error at MAX_UPLOAD_SIZE
r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
// Read the body as normal. Check for an error.
if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
// We're assuming it errored because the body is too large.
// There are other reasons it could error, you'll have to
// look at err to figure that out.
log.Println("File too large")
http.Error(w, "Your file is too powerful", http.StatusRequestEntityTooLarge)
return
}
fmt.Fprintf(w, "Upload successful")
}
有關更多詳細信息,請參閱如何在 Go 中處理文件上傳。
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報