Vant UI +Golang(gin) 上传文件

前端基本用法:点击查看

实现代码:

javascript 复制代码
const afterRead = (file) => {
  console.log(file);
  //set content-type to multipart/form-data
  const formData = new FormData();
  formData.append("file", file.file);
  request.POST("/api/v1/users/upload", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  }).then((res) => {
    console.log(res);
  });
};

后端代码:

Go 复制代码
func (h *usersHandler) Upload(c *gin.Context) {
	// 上传单张图片
	file, err := c.FormFile("file")
	if err != nil {
		logger.Error("FormFile error", logger.Err(err), middleware.GCtxRequestIDField(c))
		response.Error(c, ecode.InternalServerError)
		return
	}
	filename := file.Filename
	// 保存文件到static/pictures
	err = c.SaveUploadedFile(file, "./static/pictures/"+filename)
	if err != nil {
		logger.Error("SaveUploadedFile error", logger.Err(err), middleware.GCtxRequestIDField(c))
		response.Error(c, ecode.InternalServerError)
		return
	}
	response.Success(c, gin.H{"message": "upload success"})
}
相关推荐
码途行者13 小时前
每天10分钟学Go:第23课 协程(goroutine)
开发语言·后端·golang
goehou13 小时前
Gin 中间件的 c.Next 和 c.Abort,我跑了四组验证
后端·中间件·go·gin
利威尔xu15 小时前
2026-11-25-Go-panic-recover面试
面试·golang
码途行者15 小时前
每天10分钟学Go:第24课 通道-协程之间的传送带
开发语言·后端·golang
鲸渔3 天前
【AI Agent系列课程33-FastAPI 基础07】自动文档与接口调试:Swagger UI 和 ReDoc 入门
python·ui·fastapi·ai编程
codeejun4 天前
每日一Go·MySQL-22、幻读、不可重复读、脏读实战演示
开发语言·mysql·golang
码途行者4 天前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
利威尔xu4 天前
2026-11-24-Go限流器实战
后端·golang
码途行者5 天前
每天10分钟学Go:第21课 错误
开发语言·后端·golang
码途行者5 天前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang