我有一个HTML文件:
function runBatch() {
document.form2.w_yyyymm.value=document.form1.w_yyyymm.value;
window.open('', 'TheWindow2')
document.form2.submit();
document.form3.w_yyyymm.value=document.form1.w_yyyymm.value;
window.open('', 'TheWindow3')
document.form3.submit();
document.form4.w_yyyymm.value=document.form1.w_yyyymm.value;
window.open('', 'TheWindow4')
document.form4.submit();
}

<form name="form2" method="post" action="reportm02.do" ><input name="w_yyyymm" type="hidden" >
<input name="method" value="view" type="hidden" >
<input name="w_goods_id" value="9999" type="hidden" >
</form>
<form name="form3" method="post" action="reportm02.do" ><input name="w_yyyymm" type="hidden" >
<input name="method" value="view" type="hidden" >
<input name="w_goods_id" value="TX" type="hidden" >
</form>
<form name="form4" method="post" action="reportm02.do" ><input name="w_yyyymm" type="hidden" >
<input name="method" value="view" type="hidden" >
<input name="w_goods_id" value="TE" type="hidden" >
</form>
<input type="button" name="Submit3" value="run batch" onClick="runBatch();" >
&#13;
表单提交将获得一个xls文件,所以我想点击一个按钮获取所有xls文件。我尝试ajax但都失败了〜我该怎么办?谢谢!!
function ajaxSubmit(form)
{
var f = form;
var postData = [];
for (var i = 0; i < f.elements.length; i++) {
postData.push(f.elements[i].name + "=" + f.elements[i].value);
}
var xhr = new XMLHttpRequest();
xhr.open("POST", "reportm02.do", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(postData.join("&"));
}
function runBatch() {
ajaxSubmit(document.form2);
ajaxSubmit(document.form3);
ajaxSubmit(document.form4);
}
&#13;
..但没有xls文件下载到客户端。
我也尝试使用以下代码,但仍无效:
// Download a file form a url.
function saveFile(url) {
// Get file name from url.
var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
a.download = filename; // Set the file name.
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
};
xhr.open('GET', url);
xhr.send();
}
&#13;
答案 0 :(得分:0)
我尝试以下代码:
http://danml.com/download.html
并从中包含download.js,如下所示:
<script language="JavaScript" src="js/download.js"></script>
<Script language="JavaScript">
function saveFile(url,filename) {
//
// Get file name from url.
//var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
var x=new XMLHttpRequest();
x.open("GET", url, true);
x.responseType = 'blob';
x.onload=function(e){download(x.response, filename, "text/plain" ); }
x.send();
}
function runBatch() {
if (!date_check_yyyymm(document.form1.w_yyyymm, "申報年月")) {
return false;
}
saveFile("ReportMIB.do?method=view&w_yyyymm="+document.form1.w_yyyymm.value+"&w_goods_id=9999","880103_"+document.form1.w_yyyymm.value+"_9999.xls" )
saveFile("ReportMIB.do?method=view&w_yyyymm="+document.form1.w_yyyymm.value+"&w_goods_id=TX","880103_"+document.form1.w_yyyymm.value+"_TX.xls" )
saveFile("ReportMIB.do?method=view&w_yyyymm="+document.form1.w_yyyymm.value+"&w_goods_id=TE","880103_"+document.form1.w_yyyymm.value+"_TE.xls" )
.....
}
</script>
这适用于Chrome浏览器,但IE和Firefox。