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

I am uploading a small audio file to server using DART + golang. Everything kinda works fine, until I POST and go doesn't return anything. I would like to return filename so I can change the label text on the input.

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 response, alert

window.alert("upload complete"); works

3) ERROR in Chromium Console:

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

I'm quite new to GOLANG so any help will me much appreciated.

查看全部
donglian1953
donglian1953
2014/09/09 08:42
  • file-upload
  • upload
  • response
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复