github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_example_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"github.com/gogf/gf/v2/frame/g"
    11  	"github.com/gogf/gf/v2/net/ghttp"
    12  	"github.com/gogf/gf/v2/os/gfile"
    13  )
    14  
    15  func ExampleServer_Run() {
    16  	s := g.Server()
    17  	s.BindHandler("/", func(r *ghttp.Request) {
    18  		r.Response.Write("hello world")
    19  	})
    20  	s.SetPort(8999)
    21  	s.Run()
    22  }
    23  
    24  // Custom saving file name.
    25  func ExampleUploadFile_Save() {
    26  	s := g.Server()
    27  	s.BindHandler("/upload", func(r *ghttp.Request) {
    28  		file := r.GetUploadFile("TestFile")
    29  		if file == nil {
    30  			r.Response.Write("empty file")
    31  			return
    32  		}
    33  		file.Filename = "MyCustomFileName.txt"
    34  		fileName, err := file.Save(gfile.Temp())
    35  		if err != nil {
    36  			r.Response.Write(err)
    37  			return
    38  		}
    39  		r.Response.Write(fileName)
    40  	})
    41  	s.SetPort(8999)
    42  	s.Run()
    43  }