Skip to main content

Upload and Download

File Upload

File upload can be achieved through form submission or Ajax with GoFrame framework backend handling them similarly, so only form submission is demonstrated here.

Single File Upload

html/upload.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload File</title>
</head>
<body>

<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="ufile"> <br>
<input type="file" name="ufiles" multiple> <br>
<input type="submit" value="Upload">
</form>

</body>
</html>

controller/hello.go

func (c *Controller) Upload(req *ghttp.Request) {
file := req.GetUploadFile("ufile")
if file != nil {
file.Filename = "20231001.png" // Rename file as needed
name, err := file.Save("./upload")
if err == nil {
req.Response.Writeln(name)
}
}
}

Multiple File Upload

func (c *Controller) Upload(req *ghttp.Request) {
files := req.GetUploadFiles("ufiles")
if files != nil {
names, err := files.Save("./upload")
if err == nil {
req.Response.Writeln(names)
}
}
}

Apart from retrieving uploaded files from requests, if using API specification routing, files can also be retrieved as follows:

type UploadReq struct {
g.Meta `path:"/upload" method:"post"`

Ufile ghttp.UploadFile `json:"ufile"`

UFiles ghttp.UploadFiles `json:"ufiles"`
}

Using this method, if files are allowed to be empty, conversion errors may occur.

File Upload Application Example

  • Create an upload folder in the static resource directory to store uploaded files, as shown in the example resource/public/upload. The bound static directory is resource/public, so files can be accessed in the form /upload/<filename>.
  • Modify the uploaded file names to their corresponding hash values to prevent overwriting files with the same name.
  • Return the URL of the uploaded file.
func (c *Controller) Upload(ctx context.Context, r *api.UploadReq) (res *api.UploadRes, err error) {
req := g.RequestFromCtx(ctx)
file := req.GetUploadFile("ufile")
if file != nil {
var md5str string
md5str, err = gmd5.Encrypt(file)
if err != nil {
return
}
file.Filename = md5str + path.Ext(file.Filename)
name, err := file.Save("resource/public/upload")
if err == nil {
res = &api.UploadRes{
Data: "/upload/" + name,
}
}
}
return
}

File Download

ServeFile

ServeFile serves the file content to the client. If it's text or an image, it will display directly. Files that cannot be displayed directly in the browser will be downloaded.

func (c *Controller) Download(req *ghttp.Request) {
req.Response.ServeFile("upload/1.png")
}

ServeFileDownload

This method directly prompts the client to download the file and allows renaming of the downloaded file.

func (c *Controller) Download(req *ghttp.Request) {
req.Response.ServeFileDownload("upload/1.png", "download.png")
}

Upload Restrictions

To limit the size of files uploaded in a single request, configure clientMaxBodySize. Set it to 0 if no limits are needed.

config.yaml

server:
clientMaxBodySize: "0"