Initial upload *WIP*
This commit is contained in:
commit
1099791abb
0
README.md
Normal file
0
README.md
Normal file
45
fileboard.go
Normal file
45
fileboard.go
Normal file
@ -0,0 +1,45 @@
|
||||
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))
|
||||
}
|
19
index.html
Normal file
19
index.html
Normal file
@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<form
|
||||
enctype="multipart/form-data"
|
||||
action="http://localhost:8081/upload"
|
||||
method="post"
|
||||
>
|
||||
<input type="file" name="myFile" />
|
||||
<input type="submit" value="Upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user