文件上传需前端表单使用multipart/form-data编码;2. Golang后端通过ParseMultipartForm解析请求,用FormFile获取文件句柄并保存。

实现文件上传功能在Golang中非常直接,主要依赖标准库中的net/http和io包。通过解析HTTP请求中的multipart表单数据,可以读取并保存上传的文件。
前端需要一个支持文件上传的表单,使用multipart/form-data编码类型:
<form enctype="multipart/form-data" action="/upload" method="post"> <input type="file" name="uploadFile" /> <input type="submit" value="上传" /> </form>
在Golang服务端,使用http.Request.ParseMultipartForm()解析请求,并通过request.FormFile()获取文件句柄:
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "只允许POST方法", http.StatusMethodNotAllowed)
return
}
// 解析 multipart 表单,限制内存使用(例如32MB)
err := r.ParseMultipartForm(32 << 20)
if err != nil {
http.Error(w, "解析表单失败", http.StatusBadRequest)
return
}
// 获取名为 uploadFile 的文件
file, handler, err := r.FormFile("uploadFile")
if err != nil {
http.Error(w, "获取文件失败", http.StatusBadRequest)
return
}
defer file.Close()
// 创建本地文件用于保存
dst, err := os.Create("./uploads/" + handler.Filename)
if err != nil {
http.Error(w, "创建文件失败", http.StatusInternalServerError)
return
}
defer dst.Close()
// 将上传的文件内容复制到本地文件
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, "保存文件失败", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "文件 %s 上传成功", handler.Filename)
}
func main() {
// 确保上传目录存在
os.MkdirAll("./uploads", os.ModePerm)
http.HandleFunc("/upload", uploadHandler)
http.Handle("/", http.FileServer(http.Dir("."))) // 提供静态页面
fmt.Println("服务器启动在 :8080")
http.ListenAndServe(":8080", nil)
}
实际项目中需注意以下几点以提升安全性和稳定性:
立即学习“go语言免费学习笔记(深入)”;
ParseMultipartForm参数控制最大内存和磁盘缓存。uploads目录不可执行,防止上传脚本被运行。将上述代码保存为main.go,在同一目录下创建index.html包含上传表单,然后运行:
go run main.go
访问 https://www.php.cn/link/cbb686245ece57c9827c4bc0d0654a8e 即可测试上传功能。
基本上就这些。Golang的标准库已经足够支撑一个稳定、高效的文件上传服务,无需引入额外框架。关键在于正确处理错误和边界情况。
以上就是如何用Golang实现文件上传功能_Golang 文件上传处理示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号