读书人

关于JSP页面下载文件的有关问题JS高

发布时间: 2012-06-15 19:37:05 作者: rapoo

关于JSP页面下载文件的问题,JS高手来帮忙指点下
项目环境:tomcat6.0;IE浏览器
项目框架:ibatis+struts1+spring
由于项目需求,我需要做一个在页面上下载多个文件的功能,我的实现方式是这样的,第一步,通过ajax请求获取到了需要下载的文件参数的列表,第二步,在刚才的ajax请求的回调函数里调用window.open(?)函数,?处传入的参数是实现下载的action的url。
通过上面的方式实现了多个文件的下载,但现在有几个问题:
1.由于是采用window.open()的方式,所以每下载一个文件,都会弹出一个空白页面,虽然空白页面会很快自动消失,但文件多了的话,弹出多个空白页面,很不美观;
2.每下载一个文件就需要用户点一次“保存”,文件多了的话,会让用户很烦;
针对第一个问题,能不能实现不弹出空白页面;
针对第二个问题,能不能实现让用户点击“保存”一次就下载完全部的文件;
下面是实现的相关代码,请高手指点下。

JScript code
    //获取需要导出问卷答案的列表    function exportQuery()    {        var eCompanyId = $('eCompanyId').value;        var eQuestionpaperId = $('eQuestionpaperId').value;        var eTaskName = $('eTaskName').value;        var eQuestionStartime = $('eQuestionStartime').value;        var eQuestionEndtime = $('eQuestionEndtime').value;        var url ='<%=basePath%>wjStatistic.do?method=AllExportWj&companyId='+eCompanyId+'&questionpaperId='+eQuestionpaperId+'&taskName='+eTaskName+'&questionStartime='+eQuestionStartime+'&questionEndtime='+eQuestionEndtime;        url = encodeURI(encodeURI(url));        var checkName = new Request({                url:url,                method:'post',                onSuccess:function(text, xml){                     var obj=eval("("+text+")");//转换为json对象                      if(obj.msg=='1'){        //当msg为1表示查询结果又数据                        var exportList = obj.exportList;                        for(var i = 0;i<exportList.length;i++)                        {                            var exportWj = exportList[i];                            //alert(exportList.length);                            //exportOneWj(exportWj.questionpaper_id,exportWj.customer_questions_id);                            var exportURL = '<%=basePath%>/wjStatistic.do?method=OneExportWj&wjId='+exportWj.questionpaper_id+'&customQuestionId='+exportWj.customer_questions_id;                            //alert(exportURL);                            window.open(exportURL);    //实现下载                        }                        closeMsg();                        return false;                    }else{                        alert("本条件下无数据!");                    }                }        }).send();        //$('exportSearchForm').submit();            }

JAVA代码:
Java code
    public ActionForward OneExportWj(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {        ServletOutputStream os = null;        try {            os = response.getOutputStream();            response.setContentType("text/xml;charset=UTF-8");            String wjId = URLDecoder.decode(request.getParameter("wjId"), "UTF-8");            String customQuestionId = URLDecoder.decode(request.getParameter("customQuestionId"), "UTF-8");            Map map=new HashMap();            map.put("wjId", wjId);            map.put("customQuestionId", customQuestionId);            String wjJo=wjStatisticService.getAnsweredWj(map);            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            String time= sm.format(new Date());                        JSONObject jo=JSONObject.fromObject(wjJo);                        String filepath =jo.getString("title")+"_"+time+".html";    //问卷名+当前时间生成保存文件名称//             设置HTTP头:            response.reset();            response.setContentType("application/octet-stream");            String fileName = URLDecoder.decode(filepath, "utf-8");            response.addHeader("Content-Disposition", "attachment;" + "filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");                        String buff = mosaicHtml(jo);            byte[] byt = buff.getBytes();            os.write(byt);        } catch (Exception e) {            log.error(e.getMessage());        } finally {            os.flush();            os.close();        }        return null;    }     



[解决办法]
1.你可以将文件加入Response流中实现下载,而不用window.open()
2.你只能找一个插件实现多个文件打包下载.

读书人网 >JavaScript

热点推荐