英文:
How to avoid very large files
问题
大家好,我需要你们的帮助,因为我在使用Golang的表单时遇到了一个问题。假设我在HTML表单中有这样一个字段:
<input type="file" name="file" id="file">
我想在Golang中接收这个文件,我可以使用以下代码来实现:
func index(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(0)
if err != nil {
log.Print("Error")
}
file, _, _ := r.FormFile("file")
log.Print(file)
}
到目前为止,一切都很好,我认为我已经得到了这个文件。但是现在我的问题是,在保存文件之前,我如何查看文件的大小?我的意思是,如果我理解正确的话,Go文档上说:
整个请求体被解析,最多maxMemory字节的文件部分存储在内存中,其余部分存储在临时文件中。
我的理解是(如果我理解错了,请告诉我),文件被保存在服务器上,也就是计算机上。问题是,如果我的计算机空间非常有限,一个非常大的文件可能会暂时占满我的计算机空间并导致服务器崩溃。
我该如何避免这个问题?我如何在实际保存文件之前查看文件的大小?或者,我如何在没有实际文件的情况下限制上传到服务器的文件大小?
希望我表达清楚了,我重申一遍,如果我理解错了什么,请告诉我。提前感谢:D。
英文:
Hello everyone I ask for your help since I have a question in golang using forms. Suppose I have this field in an html form:
<input type="file" name="file" id="file">
And I want to receive the file in golang, which I do with the following code:
func index(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(0)
if err != nil {
log.Print("Error")
}
file, _, _ := r.FormFile("file")
log.Print(file)
}
So far so good, and I think I really have the file. But now my question is, how can I see the weight of the file before having it saved? I mean, if I understand correctly the go documentation says:
> The whole request body is parsed and up to a total of maxMemory bytes
> of its file parts are stored in memory, with the remainder stored on
> disk in temporary files.
What I understand (please tell me if I'm wrong) is that the file is saved on the server, I mean on the computer, the problem is, if I have a very limited space on my computer, a very large file could not fill my computer space temporarily and crash the server?
How can I avoid this problem? How can I see the size of the file without actually having the file? Or, how can I limit the size of the file that is uploaded to the server without having the file?
I hope I have made myself understood, and I repeat, if I have misunderstood something please tell me. Thanks in advance :D.
答案1
得分: 4
你可以使用MaxBytesReader来避免接受过大的文件。
r.Body = http.MaxBytesReader(w, r.Body, MaxAllowedSize)
err := r.ParseMultipartForm(MaxAllowedSize)
if err != nil {
fmt.Fprintf(w, "无法处理大于%dmb的文件,错误信息:%s", MaxAllowedSize/(1024*1024), err)
return
}
英文:
You can use MaxBytesReader and avoid accepting larger files.
r.Body = http.MaxBytesReader(w, r.Body, MaxAllowedSize)
err := r.ParseMultipartForm(MaxAllowedSize)
if err != nil {
fmt.Fprintf(w, "Can not handle files bigger than %dmb, failed with %s", MaxAllowedSize/(1024*1024), err)
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论