我知道这是一个非常基本的问题,但有很多实现,我无法让它们工作。
因此,在我的项目中,如果用户单击一个按钮,我将在servlet上生成一个zip文件(通过AJAX POST
调用)。当然,我希望将该文件下载到用户。
这是我的请求代码:
<button type="button" class="btn btn-info btn-lg" onclick="getZip();">
<span class="glyphicon glyphicon-download"></span> Download clusters (.zip)
</button>
以下是AJAX
的{{1}}:
POST
这是我的下载代码:
function getzip() {
$.ajax({
url:'GetZipServlet',
type:'GET',
});
}
我在控制台中收到了正确的消息,直至protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Downloading clusters.zip");
/* Generate the directory on the server, then zip it. */
ClinicoGenomic.getInstance().clustersToFiles();
ClinicoGenomic.getInstance().zipClusters();
System.out.println("Done generating the .zip");
String parent_dir = System.getProperty("catalina.base");
String filename = "clusters.zip";
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename);
ZipOutputStream zipStream = new ZipOutputStream( response.getOutputStream() );
ZipInputStream fi = new ZipInputStream(new FileInputStream(parent_dir + "/" + filename));
int i;
while ((i = fi.read())!=-1)
zipStream.write(i);
zipStream.close();
fi.close();
System.out.println(".zip file downloaded at client successfully");
}
。但是下载没有开始。这可能有什么问题?
答案 0 :(得分:0)
如果你正确发送了回复,你应该在ajax调用之后简单地处理它,如下所示:
{('F2', 'F1'): 0.9154929577464789,
('F2', 'F3'): 0.8285714285714286,
('F4', 'F1'): 0.8285714285714286,
('F4', 'F3'): 0.9130434782608695}