使用beego实现多文件上传

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

这种方式可行吗?

2个回答

4

这个方法是用来获取多个上传文件的。在控制器中使用“ctrl-f”查找“GetFiles return multi-upload files”,可以在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
    }
 }

2

看起来Beego没有方便的方法来上传多个文件。GetFile()只是从标准库中包装了FromFile()。您可能想使用标准库的reader函数:r.MultipartReader()。

在这种情况下,我通常通过调用以下方法从控制器公开响应reader和writer:

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

如何仅添加图片,就像我们在网站上看到的那样。一个显示添加图片的图标。 - Vijay Kumar
使用 r = this.Ctx.Request 而不是 r = this.Ctx.ResponseReader。 - jake_astub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接