Last active
February 25, 2020 19:39
-
-
Save erikdubbelboer/c6d4018e4ef96a886c4947991f26a0b1 to your computer and use it in GitHub Desktop.
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" | |
"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