duan198727 2013-03-08 13:52
浏览 62
已采纳

开始-真正的multipart.File是什么?

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
}
  • 写回答

3条回答 默认 最新

  • dqv2743 2013-03-09 13:18
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容