通过HTTP接收大文件

huangapple go评论103阅读模式
英文:

Receive big file via http

问题

我正在为你翻译以下内容:

我试图通过HTTP接收一个大约25MB的文件。我通过PHP和cURL将文件发送给"golang",然后尝试从"golang"接收它。
以下是代码:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "io/ioutil"
  6. )
  7. func handler(w http.ResponseWriter, r *http.Request) {
  8. buf, _ := ioutil.ReadAll(r.Body)
  9. fmt.Println(string(buf[:]))
  10. }
  11. func main() {
  12. http.HandleFunc("/", handler)
  13. http.ListenAndServe(":12100", nil)
  14. }

在这种情况下,我可以在ioutil.ReadAll的输出中看到我的文件。但是,如果我只尝试获取文件内容,就会失败 通过HTTP接收大文件

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "io/ioutil"
  6. )
  7. func handler(w http.ResponseWriter, r *http.Request) {
  8. r.ParseMultipartForm(0)
  9. fmt.Println(r.FormValue("f"))
  10. }
  11. func main() {
  12. http.HandleFunc("/", handler)
  13. http.ListenAndServe(":12100", nil)
  14. }

然而,如果文件很小(约900KB),一切都正常。通过HTTP发送大文件的正确方法是什么?

英文:

I've trying to receive big (~25Mb) file via http. From "client" i've send this file via PHP and cUrl and trying to receive it from golang.
Here is code:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "io/ioutil"
  6. )
  7. func handler(w http.ResponseWriter, r *http.Request) {
  8. buf, _ := ioutil.ReadAll(r.Body)
  9. fmt.Println(string(buf[:]))
  10. }
  11. func main() {
  12. http.HandleFunc("/", handler)
  13. http.ListenAndServe(":12100", nil)
  14. }

In this case i can see my file in ioutil.ReadAll output. But if i trying to get only file contents, it is failed 通过HTTP接收大文件

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "io/ioutil"
  6. )
  7. func handler(w http.ResponseWriter, r *http.Request) {
  8. r.ParseMultipartForm(0)
  9. fmt.Println(r.FormValue("f"))
  10. }
  11. func main() {
  12. http.HandleFunc("/", handler)
  13. http.ListenAndServe(":12100", nil)
  14. }

However file is small (~900Kb) everything is ok. What is right way to send big files via http?

答案1

得分: 3

Go将开始将大文件写入磁盘,以防止它们堵塞内存。然后,您可以使用FormFile获取磁盘上文件的句柄。完成后,应关闭句柄并使用r.MultipartForm.RemoveAll()删除临时文件。

完整代码如下:

  1. func handler(w http.ResponseWriter, r *http.Request) {
  2. r.ParseMultipartForm(0)
  3. defer r.MultipartForm.RemoveAll()
  4. fi, info, err := r.FormFile("f")
  5. defer fi.Close()
  6. fmt.Printf("接收到文件:%v", info.Filename)
  7. // 对文件进行操作
  8. }
英文:

Go will start writing large files to disk, so that they don't clog up memory.
You can then use FormFile to get the a handle to the file on disk. When you are done you should close the handle and delete the temporary file with r.MultipartForm.RemoveAll().

All together:

  1. func handler(w http.ResponseWriter, r *http.Request) {
  2. r.ParseMultipartForm(0)
  3. defer r.MultipartForm.RemoveAll()
  4. fi, info, err := r.FormFile("f")
  5. defer fi.Close()
  6. fmt.Printf("Recieved %v",info.Filename)
  7. //Do whatever with it
  8. }

huangapple
  • 本文由 发表于 2016年3月14日 15:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/35981958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定