Created
April 1, 2014 18:42
-
-
Save jonahgeorge/9920350 to your computer and use it in GitHub Desktop.
Go Handle File Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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