1 回答
TA贡献1783条经验 获得超4个赞
客户端正在尝试向服务器发送数据。服务器没有读取数据,它只是查看标题并关闭连接。客户端将此解释为“连接已重置”。这是你无法控制的。
不是检查标题,而是标题可以撒谎,用于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 关注
- 75 浏览
添加回答
举报