我正在使用 DART + golang 将一个小音频文件上传到服务器。一切都很好,直到我 POST 和 go 没有返回任何东西。我想返回文件名,以便我可以更改输入上的标签文本。
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 很陌生,所以任何帮助都会让我非常感激。
回首忆惘然
相关分类