将* multipart.FileHeader的内容读入[]字节

时间:2017-01-20 02:16:12

标签: file go byte

如何将* multipart.FileHeader中包含的文件的主体/内容读入GO中的一个字节([]字节)。

我唯一需要做的就是将内容读入一个巨大的字节片段,但当然我想要文件的确切大小。 我希望之后用md5散列文件内容。

// file is a *multipart.FileHeader gotten from http request.
fileContent, _ := file.Open()
var byteContainer []byte
byteContainer = make([]byte, 1000000)
fileContent.Read(byteContainer)
fmt.Println(byteContainer)

1 个答案:

答案 0 :(得分:1)

尝试ioutil.ReadAll

https://play.golang.org/p/FUgPAZ9w2X

在你的情况下做;

byteContainer, err := ioutil.ReadAll(fileContent) // why the long names though?
fmt.Printf("size:%d", len(byteContainer))

您可能还希望从multipart package docs看到此示例, https://play.golang.org/p/084tWn65-d