<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<html>
<head>
<title>demo33.jsp</title>
</head>
<body>
<label
for
=
"text"
>名称</label>
<input type=
"text"
id=
"text"
name=
"name"
/>
<label
for
=
"file"
>文件</label>
<input type=
"file"
id=
"file"
name=
"file"
/>
<button type=
"button"
onclick=
"ajaxUploadFile()"
>确定</button>
</body>
<script type=
"text/javascript"
>
function
ajaxUploadFile() {
var
formData =
new
FormData();
var
xmlhttp;
if
(window.XMLHttpRequest) {
xmlhttp =
new
XMLHttpRequest();
}
else
{
xmlhttp =
new
ActiveXObject(
"Microsoft.XMLHTTP"
);
}
xmlhttp.open(
"POST"
,
"/data"
,true);
xmlhttp.setRequestHeader(
"X-Requested-With"
,
"XMLHttpRequest"
);
formData.append(
"name"
,document.getElementById(
"text"
).value);
formData.append(
"file"
,document.getElementById(
"file"
).files[0]);
xmlhttp.send(formData);
xmlhttp.onreadystatechange=
function
() {
if
(xmlhttp.readyState==4) {
if
(xmlhttp.status==200) {
console.log(
"上传成功"
+xmlhttp.responseText);
}
else
{
console.log(
"上传失败"
+xmlhttp.responseText);
}
}
}
}
</script>
</html>