github.com/gogf/gf@v1.16.9/.example/net/ghttp/client/upload/server.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/gogf/gf/frame/g"
     5  	"github.com/gogf/gf/net/ghttp"
     6  )
     7  
     8  // Upload uploads files to /tmp .
     9  func Upload(r *ghttp.Request) {
    10  	saveDirPath := "/tmp/"
    11  	files := r.GetUploadFiles("file")
    12  	if _, err := files.Save(saveDirPath); err != nil {
    13  		r.Response.WriteExit(err)
    14  	}
    15  	r.Response.WriteExit("upload successfully")
    16  }
    17  
    18  // UploadShow shows uploading simgle file page.
    19  func UploadShow(r *ghttp.Request) {
    20  	r.Response.Write(`
    21      <html>
    22      <head>
    23          <title>GF Upload File Demo</title>
    24      </head>
    25          <body>
    26              <form enctype="multipart/form-data" action="/upload" method="post">
    27                  <input type="file" name="upload-file" />
    28                  <input type="submit" value="upload" />
    29              </form>
    30          </body>
    31      </html>
    32      `)
    33  }
    34  
    35  // UploadShowBatch shows uploading multiple files page.
    36  func UploadShowBatch(r *ghttp.Request) {
    37  	r.Response.Write(`
    38      <html>
    39      <head>
    40          <title>GF Upload Files Demo</title>
    41      </head>
    42          <body>
    43              <form enctype="multipart/form-data" action="/upload" method="post">
    44                  <input type="file" name="upload-file" />
    45                  <input type="file" name="upload-file" />
    46                  <input type="submit" value="upload" />
    47              </form>
    48          </body>
    49      </html>
    50      `)
    51  }
    52  
    53  func main() {
    54  	s := g.Server()
    55  	s.Group("/upload", func(group *ghttp.RouterGroup) {
    56  		group.POST("/", Upload)
    57  		group.ALL("/show", UploadShow)
    58  		group.ALL("/batch", UploadShowBatch)
    59  	})
    60  	s.SetPort(8199)
    61  	s.Run()
    62  }