gpt4 book ai didi

beego - 使用beego进行多文件上传

转载 作者:行者123 更新时间:2023-12-02 19:11:04 25 4
gpt4 key购买 nike

如何使用beego上传多个文件? GetFile 方法仅返回第一个文件名。

html:

<form action="/post/save" method="POST" enctype="multipart/form-data">
<input type="file" name="images" multiple>
</form>

在 Controller 中:

file, header, err := this.GetFile("images")
if err != nil {
log.Println("error", err)
} else {
log.Println("filename", header.Filename)
}

这样可以吗?

最佳答案

这个.GetFiles

ctrl-f "GetFiles 返回多重上传文件"

https://github.com/astaxie/beego/blob/master/controller.go
第450行

// 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 {`enter code here
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 {
enter code here

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
}
}

关于beego - 使用beego进行多文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26750457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com