我最近才开始玩围棋,所以我还是个菜鸟,如果我犯了太多错误,抱歉。我已经尝试解决这个问题很长时间了,但我只是不明白发生了什么。在我的 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
is GET
and r.Header.Get("Content-Type")
是一个空字符串,然后它继续直到第一个 if r.FormFile 返回以下错误:request Content-Type isn't multipart/form-data
我不明白为什么 r.Method 总是 GET 并且没有 Content-Type。我尝试过以多种不同的方式执行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 'uploadFile=@\"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
我正在运行的控制台go run main.go
使用curl时不输出任何内容。