dpw50696 2014-04-09 14:00
浏览 204
已采纳

http Request.FormFile:处理zip文件?

I'm writing a web server in go.

On one of the pages, the user can upload a file.

I would like to be able to handle zip files.

In the archive/zip package, I only see two functions which allow me to read from a zip archive :

  1. func OpenReader(name string) (*ReadCloser, error)
  2. func NewReader(r io.ReaderAt, size int64) (*Reader, error)

I would like to avoid writing and reading back from the disk,
if I want to use the second function, I need to know the size of the uploaded file before calling the function.

Question

I will split my question in two parts :

  1. What would be the idiomatic way to read the unzipped content of a zip file uploaded through a standard multipart/form-data html form ?

  2. How can I get the actual size of a file uploaded through a html form ?

    func(req *http.Request) {
        f, h, err := req.FormFile("fileTag")
        if err != nil {
             panic(err)
        }
        var fileSize int = ??
    
        unzipper, err := zip.NewReader(f, fileSize)
    }
    
  • 写回答

3条回答 默认 最新

  • dongyi2534 2014-04-09 14:28
    关注

    Here is a way I found to get the size :

    func(req *http.Request) {
        f, h, err := req.FormFile("fileTag")
        if err != nil {
             panic(err)
        }
        fileSize, err := f.Seek(0, 2) //2 = from end
        if err != nil {
             panic(err)
        }
        _, err = f.Seek(0, 0)
        if err != nil {
             panic(err)
        }
    
        unzipper, err := zip.NewReader(f, fileSize)
    }
    

    I don't find this solution very elegant or idiomatic.

    Isn't there some cleaner way to handle this case ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记