beego文件上传的例子
func (c *Controller) Upload() {
file, _, err := c.GetFile("file") // 获取上传的文件
if err != nil {
c.Ctx.WriteString("Failed to get file")
return
}
defer file.Close()
// 保存文件到服务器
fileLocation := "/path/to/your/upload/dir/" + file.Filename
err = c.SaveToFile("file", fileLocation)
if err != nil {
c.Ctx.WriteString("Failed to save file")
return
}
c.Ctx.WriteString("File uploaded successfully")
}
beego下载文件
func (c *Controller) Download() {
fileName := c.GetString("filename") // 获取要下载的文件名
fileLocation := "/path/to/your/upload/dir/" + fileName
// 将文件作为响应返回给客户端
c.Ctx.Output.Download(fileLocation, fileName)
}
详情教程请参考:beego文件上传和下载教程