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

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

golang server upload file response net::ERR_EMPTY_RESPONSE

问题

我正在使用DART + golang将一个小的音频文件上传到服务器。一切都运行得很好,直到我进行POST请求后,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还不太熟悉,所以非常感谢任何帮助。

英文:

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

  1. 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

得分: 1

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

defer file.Close()

在检查

if err != nil

之前出现了。

-- 更新

在DART中缺少第二部分:

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

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");

huangapple
  • 本文由 发表于 2014年9月9日 16:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/25740162.html
匿名

发表评论

匿名网友

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

确定