如何在 GO 中将 *multipart.FileHeader 中包含的文件的正文/内容读取到字节 ([]byte) 中。
我要做的唯一一件事就是将内容读取到一个巨大的 byte slice 中,但我当然想要文件的确切大小。 之后我想用 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)
最佳答案
https://play.golang.org/p/FUgPAZ9w2X .
在你的情况下做;
byteContainer, err := ioutil.ReadAll(fileContent) // you may want to handle the error
fmt.Printf("size:%d", len(byteContainer))
您可能还想从 multipart
查看此示例 package docs ,
https://play.golang.org/p/084tWn65-d
关于file - 将 *multipart.FileHeader 的内容读入 []byte,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41755011/