2018-04-30 22:14:14 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
|
"github.com/gogf/gf/net/ghttp"
|
2018-04-30 22:14:14 +08:00
|
|
|
)
|
|
|
|
|
|
2019-11-06 10:39:59 +08:00
|
|
|
// Upload uploads files to /tmp .
|
2018-04-30 22:14:14 +08:00
|
|
|
func Upload(r *ghttp.Request) {
|
2020-02-26 00:48:27 +08:00
|
|
|
saveDirPath := "/tmp/"
|
2020-06-03 00:09:51 +08:00
|
|
|
files := r.GetUploadFiles("file")
|
2020-04-30 20:37:09 +08:00
|
|
|
if _, err := files.Save(saveDirPath); err != nil {
|
2020-02-26 00:48:27 +08:00
|
|
|
r.Response.WriteExit(err)
|
2019-10-31 23:37:33 +08:00
|
|
|
}
|
2020-02-26 00:48:27 +08:00
|
|
|
r.Response.WriteExit("upload successfully")
|
2018-04-30 22:14:14 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-06 10:39:59 +08:00
|
|
|
// UploadShow shows uploading simgle file page.
|
2018-04-30 22:14:14 +08:00
|
|
|
func UploadShow(r *ghttp.Request) {
|
2019-04-03 00:03:46 +08:00
|
|
|
r.Response.Write(`
|
2018-04-30 22:14:14 +08:00
|
|
|
<html>
|
|
|
|
|
<head>
|
2019-11-06 10:39:59 +08:00
|
|
|
<title>GF Upload File Demo</title>
|
2018-04-30 22:14:14 +08:00
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<form enctype="multipart/form-data" action="/upload" method="post">
|
|
|
|
|
<input type="file" name="upload-file" />
|
|
|
|
|
<input type="submit" value="upload" />
|
|
|
|
|
</form>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
`)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 10:39:59 +08:00
|
|
|
// UploadShowBatch shows uploading multiple files page.
|
|
|
|
|
func UploadShowBatch(r *ghttp.Request) {
|
|
|
|
|
r.Response.Write(`
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>GF Upload Files Demo</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<form enctype="multipart/form-data" action="/upload" method="post">
|
|
|
|
|
<input type="file" name="upload-file" />
|
|
|
|
|
<input type="file" name="upload-file" />
|
|
|
|
|
<input type="submit" value="upload" />
|
|
|
|
|
</form>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
`)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 22:14:14 +08:00
|
|
|
func main() {
|
2019-04-03 00:03:46 +08:00
|
|
|
s := g.Server()
|
2019-12-02 23:00:48 +08:00
|
|
|
s.Group("/upload", func(group *ghttp.RouterGroup) {
|
2020-02-26 00:48:27 +08:00
|
|
|
group.POST("/", Upload)
|
2019-12-02 23:00:48 +08:00
|
|
|
group.ALL("/show", UploadShow)
|
|
|
|
|
group.ALL("/batch", UploadShowBatch)
|
2019-10-31 23:37:33 +08:00
|
|
|
})
|
2019-04-03 00:03:46 +08:00
|
|
|
s.SetPort(8199)
|
|
|
|
|
s.Run()
|
|
|
|
|
}
|