forked from Xzya/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.58 KB
/
main.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
"strconv"
"time"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/view"
)
func main() {
app := iris.New()
app.Adapt(iris.DevLogger())
app.Adapt(httprouter.New())
app.Adapt(view.HTML("./templates", ".html"))
// Serve the form.html to the user
app.Get("/upload", func(ctx *iris.Context) {
//create a token (optionally)
now := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(now, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
//render the form with the token for any use you like
ctx.Render("upload_form.html", token)
})
// Handle the post request from the upload_form.html to the server
app.Post("/upload", iris.LimitRequestBodySize(10<<20),
func(ctx *iris.Context) {
// or use ctx.SetMaxRequestBodySize(10 << 20)
//to limit the uploaded file(s) size.
// Get the file from the request
file, info, err := ctx.FormFile("uploadfile")
if err != nil {
ctx.HTML(iris.StatusInternalServerError,
"Error while uploading: <b>"+err.Error()+"</b>")
return
}
defer file.Close()
fname := info.Filename
// Create a file with the same name
// assuming that you have a folder named 'uploads'
out, err := os.OpenFile("./uploads/"+fname,
os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
ctx.HTML(iris.StatusInternalServerError,
"Error while uploading: <b>"+err.Error()+"</b>")
return
}
defer out.Close()
io.Copy(out, file)
})
// start the server at 127.0.0.1:8080
app.Listen(":8080")
}