title: Go - accepting http post multipart file(s)
body: <p>I am trying to figure out how to accept/receive a HTTP Post in Go. I just want to be able to receive a file, grab its mime-type and save the file locally.</p>
  
  <p>I've been searching all day but all I can find is how to send a file to some remote location but none of the examples I find cover receiving it.</p>
  
  <p>Any help would be appreciated. </p>
  
  <p>using Justinas' example and mixing with my existing experiment I've gotten this far but m.Post never seems to be called.</p>
  
  <pre><code>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()
  }
  </code></pre>