Created
March 25, 2022 12:58
-
-
Save jeffotoni/16474dea954713d7623cb71fe1998d9f to your computer and use it in GitHub Desktop.
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/ioutil" | |
"net/http" | |
) | |
func uploadFile(w http.ResponseWriter, r *http.Request) { | |
fmt.Println("File Upload Endpoint Hit") | |
// Parse our multipart form, 10 << 20 specifies a maximum | |
// upload of 10 MB files. | |
r.ParseMultipartForm(10 << 20) | |
// FormFile returns the first file for the given key `myFile` | |
// it also returns the FileHeader so we can get the Filename, | |
// the Header and the size of the file | |
file, handler, err := r.FormFile("myFile") | |
if err != nil { | |
fmt.Println("Error Retrieving the File") | |
fmt.Println(err) | |
return | |
} | |
defer file.Close() | |
fmt.Printf("Uploaded File: %+v\n", handler.Filename) | |
fmt.Printf("File Size: %+v\n", handler.Size) | |
fmt.Printf("MIME Header: %+v\n", handler.Header) | |
// Create a temporary file within our temp-images directory that follows | |
// a particular naming pattern | |
tempFile, err := ioutil.TempFile("temp-images", "upload-*.png") | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer tempFile.Close() | |
// read all of the contents of our uploaded file into a | |
// byte array | |
fileBytes, err := ioutil.ReadAll(file) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// write this byte array to our temporary file | |
tempFile.Write(fileBytes) | |
// return that we have successfully uploaded our file! | |
fmt.Fprintf(w, "Successfully Uploaded File\n") | |
} | |
func setupRoutes() { | |
http.HandleFunc("/upload", uploadFile) | |
http.ListenAndServe(":8080", nil) | |
} | |
func main() { | |
fmt.Println("Hello World") | |
setupRoutes() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment