2014-09-30 21:56
浏览 211

获取使用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条回答 默认 最新

  • doumian0569 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
    打赏 评论

相关推荐 更多相似问题