Go - accept multi-user file (s)

I am trying to figure out how to receive / receive an HTTP message in Go. I just want to get the file, capture its mime type and save the file locally.

I searched all day, but all I can find is how to send a file to a remote location, but not one of the examples that I find has received it.

Any help would be greatly appreciated.

using Justinas example and mixing it with my existing experiment, I got this far, but m.Post is never called.

package main import ( "fmt" "io" "net/http" "os" "github.com/codegangsta/martini" "github.com/codegangsta/martini-contrib/render" ) func main() { m := martini.Classic() m.Use(render.Renderer(render.Options{ Directory: "templates", // Specify what path to load the templates from. Layout: "layout", // Specify a layout template. Layouts can call {{ yield }} to render the current template. Charset: "UTF-8", // Sets encoding for json and html content-types. })) m.Get("/", func(r render.Render) { fmt.Printf("%v\n", "g./") r.HTML(200, "hello", "world") }) m.Get("/:who", func(args martini.Params, r render.Render) { fmt.Printf("%v\n", "g./:who") r.HTML(200, "hello", args["who"]) }) m.Post("/up", func(w http.ResponseWriter, r *http.Request) { fmt.Printf("%v\n", "p./up") file, header, err := r.FormFile("file") defer file.Close() if err != nil { fmt.Fprintln(w, err) return } out, err := os.Create("/tmp/file") if err != nil { fmt.Fprintf(w, "Failed to open the file for writing") return } defer out.Close() _, err = io.Copy(out, file) if err != nil { fmt.Fprintln(w, err) } // the header contains useful info, like the original file name fmt.Fprintf(w, "File %s uploaded successfully.", header.Filename) }) m.Run() } 
+8
post go multipart
source share
1 answer

The go net/http server does this pretty, using the mime/multipart package backstage. You only need to call r.FormFile() on your *http.Request to return multipart.File .

Here is a complete example . And the result of downloading the file with curl:

 justinas@ubuntu /tmp curl -i -F file=@/tmp/stuff.txt http://127.0.0.1:8080/ HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Tue, 24 Dec 2013 20:56:07 GMT Content-Length: 37 Content-Type: text/plain; charset=utf-8 File stuff.txt uploaded successfully.% justinas@ubuntu /tmp cat file kittens! 
+8
source

All Articles