1 回答

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
客戶端正在嘗試向服務(wù)器發(fā)送數(shù)據(jù)。服務(wù)器沒(méi)有讀取數(shù)據(jù),它只是查看標(biāo)題并關(guān)閉連接。客戶端將此解釋為“連接已重置”。這是你無(wú)法控制的。
不是檢查標(biāo)題,而是標(biāo)題可以撒謊,用于http.MaxBytesReader
讀取實(shí)際內(nèi)容,但如果它太大,則會(huì)出錯(cuò)。
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")
}
有關(guān)更多詳細(xì)信息,請(qǐng)參閱如何在 Go 中處理文件上傳。
- 1 回答
- 0 關(guān)注
- 100 瀏覽
添加回答
舉報(bào)