小弟因为工作需要,需要实现一个下载功能,目前功能已经实现,但是还是存在一些疑问,先把代码贴出来吧,如下,
<script type="text/javascript">
$(document).ready(function(){
$("#exBtn").click(function(){
$.get("<%=basePath%>/DownloadServlet?methodName=downloadTemplate");
});
});
</script>
</head>
<body>
<a href="<%=basePath%>/DownloadServlet?methodName=downloadTemplate">Download</a>
<input type="button" id="exBtn" value="下载模板" />
</body>
现在出现的问题就是,我点击下载button或者下载的url(a标签),都能调用到后台的的下载代码,但是通过点击button后台代码虽然执行了,但是前台是没有反应的,而点击url(a标签)则能够正常下载,请问这是为什么呢,求大神解答,感激不尽。。。
下面是后台的部分代码:
FileInputStream in = null;
ServletOutputStream out = null;
if (file.exists()) {
try{
out = response.getOutputStream();
in = new FileInputStream(file);
response.setHeader("Content-Disposition", "attachment;filename=template.xls");
response.setContentType("application/x-msdownload");
response.setContentLength((int) file.length());
byte[] bytes = new byte[1024];
int len = 0;
while ((len = in.read(bytes)) != -1) {
System.out.println("Beginning write stream, length:" + len);
out.write(bytes, 0, len);
}