Skip to content

Instantly share code, notes, and snippets.

@jonahgeorge
Created April 1, 2014 18:42
Show Gist options
  • Save jonahgeorge/9920350 to your computer and use it in GitHub Desktop.
Save jonahgeorge/9920350 to your computer and use it in GitHub Desktop.
Go Handle File Upload
package main
import (
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/upload", Process())
http.Handle("/", r)
http.ListenAndServe(":3000", nil)
}
func Process() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("file")
if err != nil {
log.Printf("%s", err)
}
fmt.Printf("%s\n%+v\n", header.Filename, file)
b, err := ioutil.ReadAll(file)
if err != nil {
log.Printf("%s", err)
}
ioutil.WriteFile(header.Filename, b, 0x777)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment