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"})
}
相关推荐
海涛高软14 小时前
Qt的ui文件的编译和使用
ui
&芒果冰沙&20 小时前
【Axure RP】什么是Axure?Axure可以用来做什么?
ui·axure·ux
不老刘20 小时前
基于LiveKit Go 实现腾讯云实时音视频功能
golang·腾讯云·实时音视频
Code季风21 小时前
Gin Web 层集成 Viper 配置文件和 Zap 日志文件指南(下)
前端·微服务·架构·go·gin
Code季风21 小时前
Gin Web 服务集成 Consul:从服务注册到服务发现实践指南(下)
java·前端·微服务·架构·go·gin·consul
简佐义的博客1 天前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
恋喵大鲤鱼2 天前
Golang 运算符
golang·运算符
weixin_437398212 天前
转Go学习笔记(2)进阶
服务器·笔记·后端·学习·架构·golang
ac.char2 天前
Golang读取ZIP压缩包并显示Gin静态html网站
golang·html·gin