form+iframe+file 页面无刷新上传文件并获取返回值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="aa">上传</button>
<form id="ajaxUploadform" action="/api/upload" role="form" method="post" target="ajaxIframe" enctype="multipart/form-data">
<input type="file" id="ajaxFile" name="ajaxFile"/></form>
<iframe name='ajaxIframe' id='ajaxIframe' ></iframe>
<script>
var aa=document.getElementById("aa");
aa.addEventListener("click",function(){
document.getElementById("ajaxUploadform").submit()
document.getElementById("ajaxIframe").onload=function(){
var result=document.getElementById('ajaxIframe').contentWindow.document.body.innerText;
alert(result)
}
},false)
</script>
</body>
</html>
请爱好前端技术的朋友,联系我,有问题大家一起讨论