1. Using Beego to achieve static file downloads
Beego is a powerful Go Web framework that provides the ability to work with static files. With a simple configuration, we can use a local folder as a static resource directory and provide users with a download link.
1.1 Configuring static file paths
First, in the In this case, we use the
SetStaticPath
localstaticfiles
Catalogs are mapped to static resource paths that can be accessed via URLs.
package main import ( "/beego/beego/v2/server/web" ) func main() { // Set static resource paths, mapping /staticfiles to the local . /staticfiles folder ("/staticfiles", "./staticfiles") () }
With this code, we are putting the local./staticfiles
directory is mapped to thehttp://localhost:8080/staticfiles
If you want to access the file, you can use this URL to access the file directly.
2. File catalog display and download function
Next, in order to allow users to easily browse the file directory and download files, we need to implement a controller to display a list of files in a specified directory and generate the corresponding download links.
2.1 Realization of the controller
In Beego, the controller is responsible for handling routing requests. We create aFileController
which defines theGet
method to read the specified directory and pass the list of files to the template.
package controllers import ( "os" "/beego/beego/v2/server/web" ) type FileController struct { } // @router /getfiles [get] func (c *FileController) Get() { // Path to the directory to be displayed dirPath := "./staticfiles" // Read the contents of the catalog files, err := (dirPath) if err != nil { ["error"] = "Unable to read directory." + () = "" return } // Passing a list of files to a template ["files"] = files ["directory"] = dirPath = "" }
In the code above, the function is used to read the
staticfiles
All files and folders in the directory. If an error occurs, render the template and display an error message. Otherwise, pass the list of files to the
Templates for display.
ns := ("/v1", ("/file", (&{})), ) (ns)
Register this Contorller in the router
2.2 Template File Display Catalog
In order to display the list of files and provide the download function, we create a simple HTML template, displays the file names to the user and generates a corresponding download link for each file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>file directory</title> </head> <body> <h1>Download file</h1> <ul> {{range .files}} {{if not .IsDir}} <li> <a href="/staticfiles/{{.Name}}" download="{{.Name}}"> {{.Name}} </a> </li> {{end}} {{end}} </ul> </body> </html>
In this template, the Go template syntax is used to iterate through thefiles
List. For each file, generate a<a>
tag and use thedownload
Properties provide file downloads.
3. Error handling page
If an error occurs while reading the catalog, we render an error page. This page displays the error message and prompts the user to go back or retry.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>error page</title> <style> body { font-family: Arial, sans-serif; margin: 50px; } .error-container { max-width: 600px; margin: auto; padding: 20px; border: 1px solid #f5c6cb; background-color: #f8d7da; color: #721c24; } h1 { color: #721c24; } </style> </head> <body> <div class="error-container"> <h1>error occurs</h1> <p>{{.error}}</p> <p>Please return and try again。</p> </div> </body> </html>
The template is passed through the{{.error}}
Renders error messages passed from the controller and makes them easier to understand with simple styling.
4. Mapping static folders with Docker
To make folder management more flexible and to enable persistent storage of static files in containerized applications, we can map local folders inside containers via Docker.
4.1 Docker Mapping Folders
exist In the
volumes
option places the host'sstaticfiles
folder is mapped to a container in the/app/staticfiles
Catalog.
version: '3'
services:
web:
image: your-beego-image
ports:
- "8080:8080"
volumes:
- /d/commanddemo/staticfiles:/app/staticfiles
Here./d/commanddemo/staticfiles
is the path to the folder on the host./app/staticfiles
is the path inside the container. In this way, files in the host and container can be kept synchronized, and any updates to the files are immediately reflected in the container.
5. Run the Beego project
After completing the above steps, you can run the Beego project. Visithttp://localhost:8080/getfiles
You will see a list of files in the directory and can download them directly.