Max File Upload Help

agolangf · · 785 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi everyone, I hoping someone has an answer for me. Kinda a golang newbie, so be gentle :) I&#39;m creating a website that allows users to upload files, but I don&#39;t want stuff going over 50MB. I&#39;ve been playing around with &#34;r.Body = http.MaxBytesReader(w, r.Body, 50 * 1024 * 1024)&#34; with the &#34;r.FormFile&#34;, but I guess I&#39;m just not understanding what&#39;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&#39;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&#39;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(&#34;file&#34;) 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 &#34;The connection was reset&#34; 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, &#34;/myfiles&#34;, 301) } file, header, err := r.FormFile(&#34;file&#34;) defer file.Close() if err != nil { log.Println(err) } out, err := os.Create(&#34;./files/&#34; + 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, &#34;/myfiles&#34;, 301) </code></pre> <p>I&#39;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 &#34;connection reset&#34; 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 &#34;Content-Length&#34; 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&#39;t send you the &#34;Content-Length&#34; into the request header, you can fall-back on the &#34;MaxBytesReader&#34;.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

785 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传