我最近开始玩去了,所以我仍然是一个小菜,抱歉,如果我犯了太多的错误。我一直试图解决这个问题很长一段时间,但我只是不明白发生了什么。在我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'}'就足够了,我不需要