Golang模板上传文件如何验证文件是否为空

huangapple go评论90阅读模式
英文:

golang template upload file how to validate the file is empty

问题

我是一个go-lang的初学者。当我使用HTML模板上传文件时遇到了一个问题。我在Google上搜索了很多但是没有解决。

使用func (*Request) FormFile来获取文件。

file, header, err := req.FormFile("receipt")

但是如何在服务器端验证文件是否为空呢?我知道可以读取request.Body来查找myfile是否为空。

有没有更好的实现方法呢?

英文:

I am a beginner with go-lang. I met a problem when I uploaded a file with html template. I google a lot but not solved.

<input  type="file" name="myfile"/>

Use func (*Request) FormFile get the file.

file, header, err := req.FormFile("receipt")

But how to validate the file whether it is empty from server side? I know I can read request.Body to find whether myfile is empty.

Is there a better way to implement it?

答案1

得分: 3

我认为在读取文件之前是不可能知道文件的大小的。请参考这个答案

读取文件内容是唯一可靠的方法。话虽如此,如果内容长度存在且太大,关闭连接是一个合理的做法。

所以我猜你需要将一部分内容读入一个小的临时缓冲区,并查看大小。

如果你想验证用户是否发送了文件,你可以检查http.ErrMissingFile

file, header, err := r.FormFile("f")
switch err {
case nil:
    // 什么都不做
case http.ErrMissingFile:
    log.Println("没有文件")
default:
    log.Println(err)
}
英文:

I don't think it's possible to know the size of the file until you read it. See this answer:

>To read the file content is the only reliable way. Having said that, if the content-lenght is present and is too big, to close the connection would be a reasonable thing to do.

So I guess you'll have to read a part of the content into a small temporary buffer and look at the size.

If you want to validate, whether the user even sent a file, you can check against http.ErrMissingFile:

	file, header, err := r.FormFile("f")
	switch err {
	case nil:
		// do nothing
	case http.ErrMissingFile:
		log.Println("no file")
	default:
		log.Println(err)
	}

答案2

得分: 1

不,你无法在不读取文件的情况下找到文件的长度。(你可以尝试使用Content-Length头部,但你必须知道它可以被修改,因此不可靠)。

以下是获取文件大小的方法:

file, handler, err := r.FormFile("receipt") // r是*http.Request
var buff bytes.Buffer
fileSize, err := buff.ReadFrom(file)
fmt.Println(fileSize) // 这将返回文件的大小。
英文:

No, you can't find the length of a file without reading it. (You can try to use Content-Length header, but you have to know that it can be modified and thus not reliable).

Here is how you can get the file size:

file, handler, err := r.FormFile("receipt") // r is *http.Request
var buff bytes.Buffer
fileSize, err := buff.ReadFrom(file)
fmt.Println(fileSize) // this will return you a file size.

答案3

得分: 0

你可以从头部变量(在你的示例中)中读取文件大小,该变量是由FileHeader类型返回的,由FormValue返回,并包含文件的大小:

file, header, err := req.FormFile("receipt")
if err != nil || header.Size == 0 {
    // 文件未发送
} else {
    // 处理文件
}

尽管我不确定这个数据在安全方面有多可靠(我猜攻击者可以恶意伪造发送到服务器的头部)。

英文:

You can read the file size from the header variable (in your example), which is a FileHeader type, returned by FormValue, and contains the size of the file:

file, header, err := req.FormFile("receipt")
if err != nil || header.Size == 0 {
    // file was not sent
} else {
    // process file
}

Though I'm not sure how reliable is this data in terms of security (I guess an attacker could fake the headers sent to the server maliciously).

huangapple
  • 本文由 发表于 2015年11月30日 16:35:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/33994374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定