我正在使用DART+golang将一个小音频文件上传到服务器。一切都很好,直到我发帖并没有返回任何东西。我想返回文件名,这样我就可以更改输入的标签文本。
1) 戈兰:
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很陌生,所以任何帮助我都将不胜感激。
上面代码中的第一个错误:
defer file.Close()
在检查之前
if err != nil
--更新
和缺失的第2部分,在DART中:
req.setRequestHeader("Content-type","multipart/form-data");