forked from SocialHW/FileUploadHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextUpload.go
More file actions
51 lines (41 loc) · 957 Bytes
/
TextUpload.go
File metadata and controls
51 lines (41 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"net/http"
"log"
"io/ioutil"
"fmt"
)
func main(){
http.HandleFunc("/", foo)
http.ListenAndServe(":8080", nil)
log.Print("Server started on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func foo(w http.ResponseWriter, req *http.Request){
var s string
if req.Method == http.MethodPost {
f, _, err := req.FormFile("userFile")
if err != nil {
log.Println(err)
http.Error(w, "UPLOAD_ERROR", http.StatusInternalServerError)
return
}
defer f.Close()
bs, err := ioutil.ReadAll(f)
if err != nil {
log.Println(err)
http.Error(w, "ERROR_READING_FILE", http.StatusInternalServerError)
return
}
s = string(bs)
}
w.Header().Set("CONTENT-TYPE", "text/html; charset=UTF-8")
fmt.Fprintf(w,`<form action="/" method="post" enctype="multipart/form-data">
Upoad a File<br>
<input type="file" name="userFile"><br>
<input type="submit">
</form>
<br>
<br>
<h1>%v</h1>`,s)
}