Skip to content

Instantly share code, notes, and snippets.

@erikdubbelboer
Last active February 25, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikdubbelboer/c6d4018e4ef96a886c4947991f26a0b1 to your computer and use it in GitHub Desktop.
Save erikdubbelboer/c6d4018e4ef96a886c4947991f26a0b1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
)
// This code can be found at: https://git.io/JvuTw
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
fmt.Fprintf(w, `
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
`)
})
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, fmt.Sprintf("file error: %v", err), http.StatusBadRequest)
return
}
// 1. Return the size of the uploaded image (as plain text).
// 2. Add a JSON response that returns the size of the uploaded file.
// Tip: use ioutil.ReadAll to read the file into a []byte.
// 3. Add the MD5 of the image to the response
// Tip: https://golang.org/pkg/crypto/md5/#Sum
// 4. Add the image dimensions to the output.
// Tip: https://golang.org/pkg/image/png/
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment