我最近開始玩去了,所以我仍然是一個小菜,抱歉,如果我犯了太多的錯誤。我一直試圖解決這個問題很長一段時間,但我只是不明白髮生了什麼。在我main.go文件我有一個主要功能:手柄文件上傳去吧
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/submit/", submit)
log.Fatal(http.ListenAndServe(":8080", nil))
}
處理函數如下:
func handler(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadFile("web/index.html")
w.Write(data)
}
我知道這不是服務於網站 的提交功能,外觀的最佳方式像這樣:
func submit(w http.ResponseWriter, r *http.Request) {
log.Println("METHOD IS " + r.Method + " AND CONTENT-TYPE IS " + r.Header.Get("Content-Type"))
r.ParseMultipartForm(32 << 20)
file, header, err := r.FormFile("uploadFile")
if err != nil {
json.NewEncoder(w).Encode(Response{err.Error(), true})
return
}
defer file.Close()
out, err := os.Create("/tmp/file_" + time.Now().String() + ".png")
if err != nil {
json.NewEncoder(w).Encode(Response{err.Error(), true})
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
json.NewEncoder(w).Encode(Response{err.Error(), true})
return
}
json.NewEncoder(w).Encode(Response{"File '" + header.Filename + "' submited successfully", false})
}
的問題是在執行提交功能時,r.Method
是GET
和r.Header.Get("Content-Type")
是空字符串,然後它繼續,直到第一個如果r.FormFile返回以下錯誤: request Content-Type isn't multipart/form-data
我不明白爲什麼r.Method總是GET並且沒有內容類型。我試圖用許多不同的方法來完成index.html,但r.Method總是GET,而Content-Type是空的。下面是上傳文件index.html中的功能:
function upload() {
var formData = new FormData();
formData.append('uploadFile', document.querySelector('#file-input').files[0]);
fetch('/submit', {
method: 'post',
headers: {
"Content-Type": "multipart/form-data"
},
body: formData
}).then(function json(response) {
return response.json()
}).then(function(data) {
window.console.log('Request succeeded with JSON response', data);
}).catch(function(error) {
window.console.log('Request failed', error);
});
}
而這裏的HTML:
<input id="file-input" type="file" name="uploadFile" />
注意,標籤不標籤裏面,我覺得可能是這個問題,所以我改變了功能和HTML這樣的事情:
function upload() {
fetch('/submit', {
method: 'post',
headers: {
"Content-Type": "multipart/form-data"
},
body: new FormData(document.querySelector('#form')
}).then(function json(response) {
return response.json()
}).then(function(data) {
window.console.log('Request succeeded with JSON response', data);
}).catch(function(error) {
window.console.log('Request failed', error);
});
}
<form id="form" method="post" enctype="multipart/form-data" action="/submit"><input id="file-input" type="file" name="uploadFile" /></form>
但這並沒有奏效。我已經用Google搜索瞭如何使用fetch()以及如何從go上接收文件上傳,我已經看到它們與我的非常相似,我不知道我在做什麼錯誤。
UPDATE: 使用curl -v -F '[email protected]\"C:/Users/raul-/Desktop/test.png\"' http://localhost:8080/submit
後,我得到以下輸出:
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /submit HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.45.0
> Accept: */*
> Content-Length: 522
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=---------------------------a17d4e54fcec53f8
>
< HTTP/1.1 301 Moved Permanently
< Location: /submit/
< Date: Wed, 18 Nov 2015 14:48:38 GMT
< Content-Length: 0
< Content-Type: text/plain; charset=utf-8
* HTTP error before end of send, stop sending
<
* Closing connection 0
使用curl時,在那裏我遇到go run main.go
輸出沒有控制檯。
第一步,使用curl檢查服務器是否正常。 –
您需要在窗體標籤上設置方法和enctype屬性。輸入標籤與GET/POST決定,多部分編碼和內容類型設置無關。 – Volker
@Volker我認爲在獲取中指定'method:post'和'header:{'Content-Type':'multipart/form-data'}'就足夠了,我不需要