golang服务器上传文件响应网:: ERR_EMPTY_RESPONSE

时间:2014-09-09 08:42:25

标签: file-upload upload go response

我正在使用DART + golang将小型音频文件上传到服务器。一切都很好,直到我发布,并没有返回任何东西。我想返回文件名,以便我可以更改输入上的标签文本。

1)GOLANG:

import (
    "encoding/json"
    "io/ioutil"
    "log"
    "net/http"
    "time"

    "fmt"
    "os"
    "io"
)

http.HandleFunc("/upload", webUploadHandler)

[...] 

func webUploadHandler(w http.ResponseWriter, r *http.Request) {

   file, header, err := r.FormFile("file") // the FormFile function takes in the POST input id file
   defer file.Close()

   if err != nil {
      fmt.Fprintln(w, err)
      return
   }

   out, err := os.Create("/tmp/uploadedfile")

   if err != nil {
      fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
      return
   }


   defer out.Close()

   // write the content from POST to the file
   _, err = io.Copy(out, file)
   if err != nil {
      fmt.Fprintln(w, err)
   }

   fmt.Fprintf(w,"File uploaded successfully : ")
   fmt.Fprintf(w, header.Filename)

}

2)DART响应,提醒

window.alert("upload complete"); 作品

3)Chromium控制台中的错误:

POST http://localhost:9999/upload net::ERR_EMPTY_RESPONSE 

我对GOLANG很新,所以任何帮助都会让我非常感激。

1 个答案:

答案 0 :(得分:1)

上面代码中的第一个错误:

defer file.Close()

在检查之前

if err != nil

- 更新

在DART中

并缺少第2部分:

req.setRequestHeader("Content-type","multipart/form-data");