Skip to content

Instantly share code, notes, and snippets.

@mgenov
Created August 31, 2017 10:49
Show Gist options
  • Save mgenov/2d77a74a8dca77448020a49ac8b354ff to your computer and use it in GitHub Desktop.
Save mgenov/2d77a74a8dca77448020a49ac8b354ff to your computer and use it in GitHub Desktop.
Simple file
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