Downloading multiple files with beego

How to download multiple files from beego? The GetFile method only returns the first filename.

HTML:

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

      

in the controller:

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

      

Can you do it like this?

+3


source to share


2 answers


It doesn't look like Beego has a convenient way to upload multiple files. GetFile () simply carries FromFile () from the standard library. You probably want to use the standard library reading function: r.MultipartReader ().

In situations like this, I usually expose the reader and write the response from the controller by calling:

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

      

Now I can use the net / http package in a standard way and implement solutions external to the infrastructure.



A quick search for multiple file uploads in Go brings me to a blog post .

In the interest of protecting this response from a rotten connection, here is the author's solution:

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

      

+2


source


this.GetFiles

ctrl-f "GetFiles returns files with multiple downloads"



https://github.com/astaxie/beego/blob/master/controller.go
line 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


source







All Articles