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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器