0

I am using the wicket framework.

I have a requirement to send to the client browser several individual files (a zip file is not relevant).

I have added to my page an AJAXDownload class that extends AbstractAjaxBehavior - a solution for sending files to the client like this:

   download = new AJAXDownload(){
    @Override
    protected IResourceStream getResourceStream(){
       return new FileResourceStream(file){

        @Override
        public void close() throws IOException {
                    super.close();
                    file.delete();
        }
       };
     }};
    add(download);

At some other point in my code I am trying to initiate the download of several files to the client using an ajax request whilst looping through an arraylist of files and then each time triggering the AJAXDownload:

    ArrayList<File> labelList = printLabels();

    for(int i=0; i<labelList.size(); i++){

     file = labelList.get(i);

     //initiate the download
     download.initiate(target);                                                                 

    }

However, it is only sending just one of these files to the client. I have checked and the files have definitely been created on the server side. But only one is of them is being sent to the client.

Can anyone give me an idea what I am doing wrong?

Thanks

Alex
  • 1
  • 1

1 Answers1

0

You are doing everything correct! I don't know how to solve your problem but I'll try to explain what happens so someone else could help:

The Ajax response has several entries like: <evaluate>document.location=/some/path/to/a/file</evaluate>

wicket-ajax.js just loops over the evaluations and executes them. If there is one entry then everything is OK - you have the file downloaded. But if there are more then the browser receives several requests for changing its location in very short time. Apparently it drops all but one of them.

An obvious solution would be to use callbacks/promises - when a download finishes then trigger the next one. The problem is that there is no way how to receive a notification from the browser that such download finished. Or at least I don't know about it.

One can roll a solution based on timeouts (i.e. setTimeout) but it would be error prone.

I hope this information is sufficient for someone else to give you the solution!

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • Hi Martin - thanks for your response. In the AJAXDownload code there is a line target.appendJavaScript("setTimeout(\"window.location.href='" + url + "'\", 100);"); Is this what you are referring to? I tried setting it to target.appendJavaScript("setTimeout(\"window.location.href='" + url + "'\", 3000);"); but it didn't solve the problem. – Alex Jul 14 '15 at 18:27