donglian1953 2014-09-09 08:42
浏览 552
已采纳

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.

  • 写回答

1条回答 默认 最新

  • doushai4890 2014-09-09 10:14
    关注

    First error in the code above:

    defer file.Close()
    

    was before checking

    if err != nil
    

    -- UPDATE

    and missing part 2, in DART:

    req.setRequestHeader("Content-type","multipart/form-data");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?