FAQ
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 \n", m)

for fname, _ := range r.MultipartForm.File {
files := m.File[fname]
fmt.Printf("files: %#v \n", m)
for i, _ := range files {
//for each fileheader, get a handle to the actual file
file, err := files[i].Open()
fmt.Printf("file: %#v \n", 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 alterations to what I've got would be
appreciated!



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 30, '14 at 8:44p
activeSep 30, '14 at 8:44p
posts1
users1
websitegolang.org

1 user in discussion

Westonc: 1 post

People

Translate

site design / logo © 2022 Grokbase