<p>Hi everyone, I hoping someone has an answer for me. Kinda a golang newbie, so be gentle :)
I'm creating a website that allows users to upload files, but I don't want stuff going over 50MB. I've been playing around with "r.Body = http.MaxBytesReader(w, r.Body, 50 * 1024 * 1024)" with the "r.FormFile", but I guess I'm just not understanding what's really going on. An example would be awesome. Many thanks!</p>
<hr/>**评论:**<br/><br/>ord32: <pre><p>MaxBytesReader is similar to io.LimitReader but is intended for limiting the size of incoming request bodies. In contrast to io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a non-EOF error for a Read beyond the limit, and closes the underlying reader when its Close method is called.</p>
<p>MaxBytesReader prevents clients from accidentally or maliciously sending a large request and wasting server resources.</p>
<p>If the request Body's size has not already been limited by MaxBytesReader, the size is capped at 10MB.</p>
<pre><code>r.Body = http.MaxBytesReader(w, r.Body, 50 * 1024 * 1024)
err := r.ParseMultipartForm(5000)
if err != nil {
log.Println(err)
return
}
file, _, err := r.FormFile("file")
if err != nil {
log.Println(err)
return
}
defer file.Close()
</code></pre></pre>sudoTalon: <pre><p>Thanks <strong>ord32</strong> for the example, it has helped a lot. It appears to be working in my code, but I am having still one little issue. When I select a file to upload that is larger than the max and hit submit, my code now tells me it was too large, but then I get a "The connection was reset" page. Here is my code:</p>
<pre><code>r.Body = http.MaxBytesReader(w, r.Body, maxFileSize) //50M Max
err := r.ParseMultipartForm(maxFileSize)
if err != nil {
http.Redirect(w, r, "/myfiles", 301)
}
file, header, err := r.FormFile("file")
defer file.Close()
if err != nil {
log.Println(err)
}
out, err := os.Create("./files/" + header.Filename)
if err != nil {
log.Println(err)
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
log.Println(err)
}
http.Redirect(w, r, "/myfiles", 301)
</code></pre>
<p>I'm not posting into the same page as my form but to another HandleFunc. Could this be my issue?</p></pre>ord32: <pre><p>source: <a href="https://stackoverflow.com/a/43785190" rel="nofollow">https://stackoverflow.com/a/43785190</a>
The issue is that the client does not read the response.
When the MaxBytesReader limit is breached, the server stops reading data from the client. Also, the server fully closes the connection a half second after writing the response to the client.
Many HTTP clients write the complete request body before reading the response and stop on any error writing the request body. These clients report "connection reset" errors and the like when the request body is sufficiently large and ignore the response written by the server.</p>
<p>you can use Content-Length header
or use javascript code to determine file size before uploading the file ...</p></pre>tuxlinuxien: <pre><p>I would suggest you to first extract "Content-Length" from the header. If the body size exceeds your limit, you can directly return an error and close the connection, in this situation, it will avoid unnecessary processing.</p>
<p>Then if the client doesn't send you the "Content-Length" into the request header, you can fall-back on the "MaxBytesReader".</p></pre>