在我的使用案例中,我试图上传一个文件到golang的服务器。我有以下的HTML代码,golang文件上传失败
<div class="form-input upload-file" enctype="multipart/form-data" >
<input type="file"name="file" id="file" />
<input type="hidden"name="token" value="{{.}}" />
<a href="/uploadfile/" data-toggle="tooltip" title="upload">
<input type="button upload-video" class="btn btn-primary btn-filled btn-xs" value="upload" />
</a>
</div>
而在服务器端,
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("file")
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
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)
}
当我尝试上传的文件,我得到在服务器端request Content-Type isn't multipart/form-data
错误。
任何人都可以帮助我吗?
谢谢。我试图在另一种形式中使用这个。所以我尝试了这种方式。有没有任何工作可以在另一个表单中使用它?这将是非常有帮助 – Dany
@DineshAppavoo窗体内部窗体? –
看起来不可能[nest-forms](http://stackoverflow.com/questions/379610/can-you-nest-html-forms)。我试过了,原始形式正在崩溃。有没有解决方法? – Dany