This commit is contained in:
Calamitas 2022-07-18 23:41:28 -05:00
parent aae520106c
commit b7f722a845
2 changed files with 1 additions and 46 deletions

@ -1,4 +1,4 @@
#Shitty image upload package
# Shitty image upload package
```sh
go run image_upload.go

@ -1,45 +0,0 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
// GLOBAL
func filehandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "files")
}
func uploadhandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
fmt.Println("Upload your image here")
if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, "File is too big", http.StatusBadRequest)
}
file, handler, err := r.FormFile("myFile")
if err != nil {
fmt.Println("Cannot retrieve file")
fmt.Println(err)
return
}
defer file.Close()
fmt.Printf("Uploaded File: %+v\n", handler.Filename)
tempFile, err := ioutil.TempFile("files", "upload-*.png")
if err != nil {
fmt.Println(err)
}
defer tempFile.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
tempFile.Write(fileBytes)
fmt.Fprintf(w, "Successfully uploaded File\n")
}
func main() {
http.HandleFunc("/upload", uploadhandler)
http.HandleFunc("/files", filehandler)
log.Fatal(http.ListenAndServe(":8081", nil))
}