Created
August 31, 2017 10:49
-
-
Save mgenov/2d77a74a8dca77448020a49ac8b354ff to your computer and use it in GitHub Desktop.
Simple file
This file contains hidden or 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" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
) | |
func main() { | |
http.HandleFunc("/upload", handleUpload) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} | |
func handleUpload(w http.ResponseWriter, r *http.Request) { | |
fmt.Printf("%v\n", "file upload") | |
r.ParseMultipartForm(32 << 20) | |
file, header, err := r.FormFile("file") | |
if err != nil { | |
fmt.Fprintln(w, err) | |
return | |
} | |
defer file.Close() | |
out, err := os.Create("out.bin") | |
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment