This commit is contained in:
parent
aae520106c
commit
b7f722a845
@ -1,4 +1,4 @@
|
|||||||
#Shitty image upload package
|
# Shitty image upload package
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go run image_upload.go
|
go run image_upload.go
|
||||||
|
45
fileboard.go
45
fileboard.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))
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user