【问题标题】:Multiple files download on IE8 with Servlets使用 Servlet 在 IE8 上下载多个文件
【发布时间】:2012-08-13 02:48:42
【问题描述】:

我正在尝试构建一个可以在客户端下载多个文件(而不是 zip 文件)的网络应用程序。 我正在使用 jsf、jquery 和 servlet。 该代码在 Mozilla 中运行良好,但在 IE8 中无法运行。

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

这在 Mozilla 中运行良好,但在 IE8 中,如果您按足够快的 ESC 键,您将获得 3 个保存/打开提示 :) 这还不够... 我已经检查了所有 4 个请求和所有 4 个响应是否都已发出并且它们都在那里......我看不出提示的问题在哪里......

有什么方法可以让 jquery 等到 promt 关闭后再生成下一个 iframe(请求)?我认为这即使在 IE 中也应该有效 :)

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

小服务程序:

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>

欢迎提出任何建议!

【问题讨论】:

  • 我不确定正确的解决方案,但是如果您使用计时器来减慢触发单个请求的速度会怎样。几秒钟可能足以让它在 IE 中运行。

标签: jquery jsf servlets richfaces


【解决方案1】:

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

变量 i = 4;代表我删除的 printTest() 函数中的循环。 在每次请求/获得焦点时,我都会减少 print() 函数。 var b 是一个标志,表明这是第一个焦点并且操作按钮没有被按下。 我还更改了按钮中的操作:

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

但仍然存在一个问题: 沃尔多在哪里? (第一个示例的第 4 个文件) 在第一个示例中,正常流程是这样的:
1) 第一个文件被提示,其余文件丢失在某处
或
2)所有4个文件都得到提示

IE 会提示随机数量的文件,而不是所有文件。我真的很困惑。 当我有空闲时间时,我会在微软论坛上发布这个......也许他们对此有一个合乎逻辑的解释。 我这里也放个链接给大家。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多