简体   繁体   English

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

[英]Multiple files download on IE8 with Servlets

I am trying to build a web app which can download multiple files on the client (not in a zip file). 我正在尝试构建一个可以在客户端上下载多个文件的Web应用程序(不是在zip文件中)。 I'm using jsf, jquery and servlets. 我正在使用jsf,jquery和servlets。 The code works fine in Mozilla but does not work in IE8. 代码在Mozilla中运行良好,但在IE8中不起作用。

To be more specific: i want the client to receive 4 different save/open promts from the browser. 更具体一点:我希望客户端从浏览器接收4个不同的保存/打开承诺。 So ... i made a simple example like this: user presses the Action button >> 4 different iframes will be created with the src attribute equals to servlet path >> the servlet generates a response with a pdf file >> the client receives multiple save/open promts 所以...我做了一个这样一个简单的例子:用户按下Action按钮>>将创建4个不同的iframes,其中src属性等于servlet路径>> servlet生成带有pdf文件的响应>>客户端收到多个保存/打开承诺

This works fine in Mozilla but in IE8 if you push fast enough the ESC key you will get 3 save/open promts :) which is not enough... I have checked to see if all the 4 requests and all 4 responses are made and they are all there ... I can't see where is the problem with the prompts ... 这在Mozilla中可以正常工作但在IE8中如果你足够快地按ESC键你将得到3个保存/打开的承诺:)这还不够......我已经检查过是否所有4个请求和所有4个响应都是他们都在那里......我看不出提示的问题在哪里......

Is there any way to make the jquery wait untill the promt is closed to make the next iframe (request)? 有没有办法让jquery等到直到promt关闭才能生成下一个iframe(请求)? I think this should work even in IE :) 我认为这应该在IE中工作:)

In this example there are 4 pdfs created with JasperReports. 在此示例中,使用JasperReports创建了4个pdf。

Servlet: 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();
    }
}

}

and test.xhtml: 和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>

Any suggestions are welcomed! 欢迎任何建议!

I got really busy this weekend and forgat to post the answer. 这个周末我真的很忙,并且发布了答案。 As it turns out the javascript window object is losing focus on every promt (I knew that but it seems i`m getting realy lazy :( and start asking people to do my work )... so i can make a request / focus gained. This is a very unprofessional and "first try" example but it tricks IE :) 事实证明,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);
}

The var i = 4; var i = 4; stands for the loop in the printTest() function wich i deleted. 代表我删除的printTest()函数中的循环。 On every request/focus gained i decrements in the print() function. 在获得的每个请求/焦点上,我在print()函数中递减。 The var b is a flag to indicate if this is the first focus and the action button has not been pressed. var b是一个标志,用于指示这是否是第一个焦点,并且未按下操作按钮。 And i also changed the action in the button: 我也改变了按钮中的动作:

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

But a question still remains: Where is Waldo? 但问题仍然存在:沃尔多在哪里? (the 4th file from the first example) A normal flow, in the first example, as i see it would be like this: (第一个例子中的第四个文件)正常流程,在第一个例子中,正如我所见,它将是这样的:
1) 1st file gets promted and the rest get lost somewhere 1)第一个文件被提前,其余的文件丢失了
or 要么
2) all 4 files get promted 2)所有4个文件都被提出

IE promts a random number of files and never all of them. IE承诺随机数量的文件,而不是所有文件。 I am really confused. 我真的很困惑。 When i get some free time i will post this on the microsoft forums ... maybe they have a logical explanation for this. 当我获得一些空闲时间时,我会在微软论坛上发布这个...也许他们对此有合理的解释。 I'll put a link here too for you guys. 我也会在这里给你们一个链接。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM