Go – 什么是multipart.File?

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

Go - What is really a multipart.File?

问题

文档中提到:

> 如果存储在磁盘上,文件的底层具体类型将是*os.File。

在这种情况下一切都很清楚。很好。但是,如果文件不是存储在磁盘上,而是存储在内存中会发生什么呢?

我目前的问题是,我正在尝试获取通过HTML表单获得的存储在内存中的不同文件的大小,但是我无法使用os.Stat来执行fileInfo.Size(),因为我没有文件的位置,只有文件的名称。

fhs := req.MultipartForm.File["files"]
for _, fileHeader := range fhs {
    file, _ := fileHeader.Open()
    log.Println(len(file)) // 由于是multipart.File类型,所以会出错
    fileInfo, err  := os.Stat(fileHeader.Filename) // 由于只有文件名而不是完整路径,所以会出错
    
    // 在这里我将对文件进行操作
}
英文:

In the docs it is said that

> If stored on disk, the File's underlying concrete type will be an
> *os.File.

In this case everything is clear. Great. But, what happens if not, if the file is stored in memory?

My actual problem is that I´m trying to get the size of the different files stored in memory that I got though an html form but I can not use os.Stat to do fileInfo.Size() because I don´t have the location of the file, just it´s name.

fhs := req.MultipartForm.File["files"]
for _, fileHeader := range fhs {
    file, _ := fileHeader.Open()
	log.Println(len(file)) // Gives an error because is of type multipart.File
	fileInfo, err  := os.Stat(fileHeader.Filename) // Gives an error because it´s just the name, not the complete path
    
    // Here I would do things with the file
}

答案1

得分: 7

你可以利用multipart.File实现了io.Seeker接口的事实来找到它的大小。

cur, err := file.Seek(0, 1)
size, err := file.Seek(0, 2)
_, err := file.Seek(cur, 0)

第一行找到文件的当前偏移量。第二行将文件定位到文件末尾,并返回它相对于文件开头的位置。这就是文件的大小。第三行将文件定位到我们在尝试找到大小之前的偏移量。

你可以在这里阅读更多关于seek方法的信息。

英文:

You can exploit the fact that multipart.File implements io.Seeker to find its size.

cur, err := file.Seek(0, 1)
size, err := file.Seek(0, 2)
_, err := file.Seek(cur, 0)

The first line finds the file's current offset. The second seeks to the end of the file and returns where it is in relation to the beginning of the file. This is the size of the file. The third seeks to the offset we were at before trying to find the size.

You can read more about the seek method here.

答案2

得分: 2

如果你调用parseMultipartForm(0),这将把整个文件写入磁盘而不是存储在内存中,然后是f, _ := FormFile("file"),然后你可以使用fi, _ := f.(*os.File).Stat()来获取文件的状态。

英文:

if you call parseMultipartForm(0) this will write the entire file to disk instead of storing anything in memory, followed by f, _ := FormFile("file") then you can stat the file with fi, _ := f.(*os.File).Stat()

答案3

得分: 1

根据您对数据的处理需求,最好的做法可能是使用ioutil.ReadAll将文件读取为一个字节切片。(无论如何,您最终可能需要将数据作为字节切片处理。)一旦完成这一步骤,您可以使用len函数找到其长度。

英文:

Depending on what you want to do with the data, the best thing to do may be to read the file into a byte slice with ioutil.ReadAll. (You might want the data as a byte slice eventually, anyway.) Once you've done that, you an find the length with len.

huangapple
  • 本文由 发表于 2013年3月8日 21:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/15295522.html
匿名

发表评论

匿名网友

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

确定