View と Export の 2 つのボタンを含む単純な JSP ページがあります。[表示] ボタンをクリックすると、DB からデータを取得し、コピーをセッションに保持して、データを含むラベルに HTML コードを書き込みます。後でユーザーが [エクスポート] をクリックすると、サーバーで (セッションからのデータを使用して) Excel ファイルを生成し、それをクライアント側にダウンロードします。
サーバー側でExcelファイルが正常に作成されました。クライアント側からの AJAX リクエストを使用して、サーバーから Excel ファイルをダウンロードしています。
JSP コード:
try{
String filepath=ExportToExcel(session.getAttribute("InvestmentDetails"));
//Setting file to download
response.setContentType( "application/x-download");
response.setHeader("Content-Disposition","attachment; filename=\"SIPInvestment_531.xls\"");
response.setStatus(200);
InputStream in = null;
ServletOutputStream outs = response.getOutputStream();
try {
File filetodownload=new File(filepath);
response.setContentLength(Integer.parseInt(String.valueOf(filetodownload.length())));
in = new BufferedInputStream(new FileInputStream(filetodownload));
int ch;
while ((ch = in.read()) != -1) {
outs.print((char) ch);
}
}
finally {
if (in != null) in.close();
}
outs.flush();
outs.close();
}
catch(Exception ex){
str=ex.getMessage();
}
Javascript は次のとおりです。
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
}
xmlhttp.open("POST","/SIP/rptClientInvestmentDetails.jsp?requesttype=export",false);
xmlhttp.send();
リクエストはJSPページに届きます。そして例外なく、応答出力ストリームに書き込みます。しかし、ブラウザからダウンロードはポップアップしません。何が問題になる可能性がありますか?