最近项目中用到beego,需要实现文件批量上传,翻了好久beego的文档都没有找到满意的解决办法,结果看源码时发现作者已经给出了相关实现代码,在源码包controller.go文件中560-586行,记录如下:

//GetFiles return multi-upload files
files, err:=c.GetFiles("myfiles")
	if err != nil {
		http.Error(w, err.Error(), http.StatusNoContent)
		return
	}
for i, _ := range files {
	//for each fileheader, get a handle to the actual file
	file, err := files[i].Open()
	defer file.Close()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	//create destination file making sure the path is writeable.
	dst, err := os.Create("upload/" + files[i].Filename)
	defer dst.Close()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	//copy the uploaded file to the destination file
	if _, err := io.Copy(dst, file); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

对应的input标签需设置multiple 属性

<input type="file" multiple name="myfiles">

相关文章:

  • 2021-12-01
  • 2021-06-26
  • 2021-10-07
  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-11-13
猜你喜欢
  • 2022-02-05
  • 2022-12-23
  • 2021-11-20
  • 2021-09-29
  • 2022-01-13
相关资源
相似解决方案