2014-11-05 54 views
3

如何用beego上傳多個文件? GetFile方法只返回第一個文件名。用beego上傳多個文件

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

它似乎沒有像Beego有一個方便的方法來上傳多個文件。 GetFile()僅包含標準庫中的FromFile()。您可能想要使用標準庫的閱讀器功能:r.MultipartReader()。

在這樣的情況下,我一般是通過調用暴露從控制器的響應讀寫器:

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

現在我可以使用net/HTTP包中的標準方法和實施方案外到框架。

在Go上快速搜索上傳多個文件會將我引導至blog post

在保護這個答案從鏈接腐爛的利益,這裏是作者的解決方案:

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

如何添加圖片,就像我們在網站上看到的一樣。顯示添加圖像的圖標。 – 2015-06-09 06:50:41

+0

使用r = this.Ctx.Request代替r = this.Ctx.ResponseReader – 2016-04-19 14:16:30

2

this.GetFiles

CTRL-F 「的GetFiles返回多上傳文件」

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