2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-06-08 20:21:35 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
2020-06-08 20:21:35 +08:00
|
|
|
)
|
|
|
|
|
2021-12-03 23:32:00 +08:00
|
|
|
func ExampleServer_Run() {
|
2020-06-08 20:21:35 +08:00
|
|
|
s := g.Server()
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("hello world")
|
|
|
|
})
|
|
|
|
s.SetPort(8999)
|
|
|
|
s.Run()
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:05:30 +08:00
|
|
|
// Custom saving file name.
|
|
|
|
func ExampleUploadFile_Save() {
|
|
|
|
s := g.Server()
|
|
|
|
s.BindHandler("/upload", func(r *ghttp.Request) {
|
|
|
|
file := r.GetUploadFile("TestFile")
|
|
|
|
if file == nil {
|
|
|
|
r.Response.Write("empty file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file.Filename = "MyCustomFileName.txt"
|
2022-02-14 14:50:25 +08:00
|
|
|
fileName, err := file.Save(gfile.Temp())
|
2020-08-24 23:05:30 +08:00
|
|
|
if err != nil {
|
|
|
|
r.Response.Write(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Response.Write(fileName)
|
|
|
|
})
|
|
|
|
s.SetPort(8999)
|
|
|
|
s.Run()
|
|
|
|
}
|