3

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

html:

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

在控制器中:

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

可以这样做吗?

4

2 回答 2

4

这个.GetFiles

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

https://github.com/ataxie/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
    }
 }
于 2016-01-13T09:59:39.167 回答
2

Beego 似乎没有上传多个文件的便捷方法。GetFile() 只是从标准库中包装 FromFile()。您可能想要使用标准库的阅读器函数:r.MultipartReader()。

在这种情况下,我通常通过调用从控制器公开响应读取器和写入器:

w = this.Ctx.ResponseWriter
r = this.Ctx.ResponseReader

现在我能够以标准方式使用 net/http 包并在框架外部实施解决方案。

快速搜索在 Go 中上传多个文件会引导我找到有用的博客文章

为了保护这个答案免受链接腐烂,这是作者的解决方案:

func uploadHandler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    //GET displays the upload form.
    case "GET":
        display(w, "upload", nil)

    //POST takes the uploaded file(s) and saves it to disk.
    case "POST":
        //get the multipart reader for the request.
        reader, err := r.MultipartReader()

        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        //copy each part to destination.
        for {
            part, err := reader.NextPart()
            if err == io.EOF {
                break
            }

            //if part.FileName() is empty, skip this iteration.
            if part.FileName() == "" {
                continue
            }
            dst, err := os.Create("/home/sanat/" + part.FileName())
            defer dst.Close()

            if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }

            if _, err := io.Copy(dst, part); err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
        }
        //display success message.
        display(w, "upload", "Upload successful.")
    default:
        w.WriteHeader(http.StatusMethodNotAllowed)
    }
}
于 2015-02-05T05:28:06.457 回答