js用var form = new FormData(); 建立一个表单,代码如下:
```
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var form = new FormData();
if (form) {
var cvs = document.getElementById(cvslabel);
base64Img = cvs.toDataURL("image/jpeg");
form.append("myfile", base64Img);
}
else
alert("Ajax: Error 01");
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText != "0") {
var retext = xmlhttp.responseText.split("|");
var uploadimg = document.getElementById("uploadimg");
if (uploadimg.value == "")
uploadimg.value = retext[1];
else
uploadimg.value = uploadimg.value + '|' + retext[1];
var localimghtml = '<span id="' + retext[0] + '"><table style=\"float:left;\"><tr><td valign=\"center\" height=\"100\"><img style="margin-left:5px;max-width:200px;max-height:100px;" src="' + retext[1] + '" /></td></tr>';
localimghtml += '<tr><td align=\"center\" height=\"30\" valign=\"bottom\"><input type=\"image\" src=\"bgeditor/images/delimg.jpg\" onclick=delimg("' + retext[0] + '","' + retext[1] + '"); style=\"width:25px;\" /></td></tr></table></span>';
$("#upload_pic_list").html($("#upload_pic_list").html() + localimghtml);
if (ImgInsert > 0) {
var bbsgoodedit = document.getElementById(labelname);
bbsgoodedit.focus();//获得焦点
//localimghtml = '<img src="'+retext[1]+'" />';
//_insertimg(localimghtml,labelname)
InsertImage(retext[1]);
}
}
return true;
}
else {
return false;
}
}
xmlhttp.open("POST", postfile, false);//最后一个参数 false为同步
xmlhttp.send(form);
```
go代码如下:
```
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "0")
}
//data := r.FormValue("myfile")
data := r.Form.Get("myfile")
fmt.Fprintf(w,data)
```
上面的代码始终无法接收到myfile的内容。myfile一直是空值。
请高手指点
我用 fmt.Println(r) 已经能够打印出 myfile 的内容,因为我在xmlhttp.send前加了下面一句,
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
但是用以下代码还是无法获取
```
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "0")
}
//data := r.FormValue("myfile")
//data := r.PostFormValue("myfile")
data := r.Form.Get("myfile")
//fmt.Fprintf(w, data)
fmt.Println(data)
```
帮个忙呀
#2