使用Servlet在IE8上下载多个文件

时间:2012-08-16 17:30:36

标签: jquery jsf servlets richfaces

我正在尝试构建一个可以在客户端上下载多个文件的Web应用程序(不是在zip文件中)。 我正在使用jsf,jquery和servlets。 该代码在Mozilla中运行良好,但在IE8中不起作用。

更具体一点:我希望客户端从浏览器接收4个不同的保存/打开承诺。所以......我做了一个这样一个简单的例子: 用户按下操作按钮>>将使用src属性等于servlet路径>>创建4个不同的iframe。 servlet使用pdf文件生成响应>>客户端收到多个保存/打开承诺

这在Mozilla中工作正常但在IE8中如果你足够快地按ESC键你将得到3个保存/打开的承诺:)这还不够...... 我已经检查过是否所有4个请求和所有4个响应都已完成并且它们都在那里......我看不出提示的问题在哪里...

有没有办法让jquery等到直到promt关闭才能生成下一个iframe(请求)?我认为这应该在IE中工作:)

在此示例中,使用JasperReports创建了4个pdf。

的Servlet

public class TestServlet extends HttpServlet {
private static final long   serialVersionUID    = 509291005008276860L;
private Logger logger LoggerFactory.getLogger( TestServlet.class );

@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
{
    try {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put( "JUDET", "Alba" );
        params.put( "NUME", "Caliman" );
        params.put( "PRENUME", "Victor" );
        params.put( "DATA_NASTERE", "01/03/1987" );
        params.put( "LOCALITATE", "Alba Iulia" );
        params.put( "STR", "T. Vladimirescu" );
        params.put( "NR", "11" );
        params.put( "BL", "V4" );
        params.put( "SC", "A" );
        params.put( "ET", "1" );
        params.put( "AP", "1" );
        params.put( "JUDET_DOMICILIU", "Alba" );
        params.put( "TIP_ACT", "CI" );
        params.put( "SERIA", "Ax" );
        params.put( "NR_ACT", "42910" );
        params.put( "CNP", "1870301011193" );
        params.put( "COR", "Inginer software" );
        params.put( "BENEFICIAR", 0 );
        params.put( "EVIDENTA", 0 );
        params.put( "DATA", "02/08/2012" );

        PdfData pdfData = new PdfData();
        pdfData.setExportedFileName( "cerereDosar.pdf" );
        pdfData.setTemplateName( "reports/cerereDosar.jasper" );
        pdfData.setParams( params );

        InputStream inputStream = null;

        resp.reset();
        resp.setHeader( "Content-Type", "application/octet-stream" );
        resp.setHeader( "Content-Disposition", "attachment; filename=\"" + pdfData.getExportedFileName() + "\";" );

        inputStream = new ClassPathResource( pdfData.getTemplateName() ).getInputStream();

        //          final Locale locale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
        //          pdfData.getParams().put( JRParameter.REPORT_LOCALE, new Loca );

        JasperPrint jasperPrint = JasperFillManager.fillReport( inputStream, pdfData.getParams(),
            new JREmptyDataSource() );

        JasperExportManager.exportReportToPdfStream( jasperPrint, resp.getOutputStream() );

        inputStream.close();

        logger.info( req.getQueryString() );
    }
    catch( Exception e ) {
        e.printStackTrace();
    }
}

}

和test.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:nao="http://nao.anofm.org/jsf/composite/util"
xmlns:naocc="http://java.sun.com/jsf/composite/components">
<ui:composition template="/layout/template.xhtml">
<ui:define name="content">
    <h:form id="forma">
        <a4j:commandButton value="Action" onclick="printTest();" />
        <div style="display: none;" id="result"></div>
    </h:form>

    <script type="text/javascript">     

                function print(tipAct){
                    $('<iframe>')
                    .appendTo('#result')
                    .attr('src', 'http://localhost:8080/nao-inregistrare/TestServlet/')
                    .attr('id', tipAct);
                }

                function printTest(){
                    for(var i = 1; 5>i; i++){
                        $.when(print(i)).done();
                        }
                }

</script>
</ui:define>
</ui:composition>
</html>

欢迎任何建议!

1 个答案:

答案 0 :(得分:0)

这个周末我真的很忙,并且发布了答案。 事实证明,javascript窗口对象正在失去对每一个promt的关注(我知道,但似乎我真的很懒惰:(并开始让人们去做我的工作)...所以我可以提出请求/焦点获得这是一个非常不专业和“第一次尝试”的例子,但它欺骗IE:)

var i = 4;

window.onfocus = function(){
if(b == true && i != 0){
print(i);
} else {
alert('i`m done');
}
}

var b;

function print(tipAct){
i--;
b = true;
$('<iframe>')
.appendTo('#result')
.attr('src', 'http://localhost:8080/nao-inregistrare/TestServlet/')
.attr('id', tipAct);
}

var i = 4;代表我删除的printTest()函数中的循环。 在获得的每个请求/焦点上,我在print()函数中递减。 var b是一个标志,用于指示这是否是第一个焦点,并且未按下操作按钮。 我也改变了按钮中的动作:

<a4j:commandButton value="Action" onclick="print(i);" />

但问题仍然存在: 沃尔多在哪里? (第一个例子的第4个文件) 在第一个例子中,正常流程,如我所见,它将是这样的:
1)第一个文件被提前,其余的文件丢失了 或
2)所有4个文件都被提前

IE承诺随机数量的文件,而不是所有文件。我真的很困惑。 当我获得一些空闲时间时,我会在微软论坛上发布这个...也许他们对此有合理的解释。 我也会在这里给你们一个链接。 :)