dtrz17917 2014-09-30 21:56
浏览 228
已采纳

获取使用net / http request处理的上传文件的Content-Type标头。ParseMultipartForm

I'm using the net/http package to write a server that, among other things, receives a files uploaded via POST with the multipart/form-data encoding.

Working my way through the net/http docs, I've managed to write a function that calls the ParseMultipartForm method on the request and then interrogates the MultipartForm field for the file data, but I seem to be stuck on how to get the Content Type of the individual files uploaded as part of the post request -- all the ContentType related references in the source of request.go seem to be related to getting the multipart/form encoding, and when I printf-dump the uploaded file information, I don't seem to see any types, fields, or map keys that look content type related.

Here's what I've got right now that's doing the work of the file upload:

func save_multipart_upload(r *http.Request, savepath string) ([]string, error) {
    fmt.Println("CALL: save_multipart_upload(r,"+savepath+")")

    var savedfiles []string

    err := r.ParseMultipartForm(100000)
    if err != nil {
        return savedfiles, err
    }

    m := r.MultipartForm
    fmt.Printf("MPF: %#v 
", m)

    for fname, _ := range r.MultipartForm.File {
        files := m.File[fname]
        fmt.Printf("files: %#v 
", m)
        for i, _ := range files {
            //for each fileheader, get a handle to the actual file
            file, err := files[i].Open()
            fmt.Printf("file: %#v 
", file)
            defer file.Close()
            if err != nil {
                return savedfiles, err
            }
            //create destination file making sure the path is writeable.
            var filename string
            if savepath[:len(savepath)] == "/" {
                filename = savepath + files[i].Filename
            } else {
                filename = savepath + "/" + files[i].Filename
            }
            dst, err := os.Create(filename)
            if err != nil {
                fmt.Println("Can't create "+filename+": "+err.Error())
                return savedfiles, err
            } else if _, err := io.Copy(dst, file); err != nil {
                fmt.Println("Can't copy data %s: "+err.Error(), filename)
                return savedfiles, err
            } else {
                fmt.Println("Saved %s successfully.", filename)
                savedfiles = append(savedfiles, files[i].Filename)
            }
        }
    }
    /* end multipart upload */
    fmt.Println("RETURN: receive_multipart_upload")
    return savedfiles, err
}

And here's what I see from the print dumping if I pick a video file:

CALL: save_multipart_upload(r,./static/000000000000000000000000/video/)

MPF: &multipart.Form{Value:map[string][]string{}, File:map[string][]*multipart.FileHeader{"file1":[]*multipart.FileHeader{(*multipart.FileHeader)(0xc21004e580)}}}

files: &multipart.Form{Value:map[string][]string{}, File:map[string][]*multipart.FileHeader{"file1":[]*multipart.FileHeader{(*multipart.FileHeader)(0xc21004e580)}}}

file: &os.File{file:(*os.file)(0xc210079510)}

Saved %s successfully. ./static/000000000000000000000000/video//2012-08-03 19.31.48.mov

RETURN: receive_multipart_upload

multipart.FileHeader looks like it might be a clue, but when I drill down, that has a Header field, which is of type textproto.MIMEHeader, which basically looks like it's a map of strings to strings, so I still don't know what to look for.

Any ideas and sample code or suggested alterations to what I've got would be appreciated!

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-11-28 19:33
    关注

    It should work like this:

    m := r.MultipartForm
    header := m.MIMEHeader
    // Take a look at for instance 
    // https://en.wikipedia.org/wiki/MIME#Multipart_messages
    // You are looking for Content-Type...
    // However, it can be multivalued. That's why you get a splice.
    types, ok := header["Content-Type"]
    if ok {
      // This should be true!
      for _, x := range types {
        fmt.Printf("Content-Type: %v", x)
        // Most usually you will probably see only one
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题