So I modified my AjaxFileDownload init method to eliminate the abstraction of
getResourceStream. Code as follows:

AjaxFileDownload.java... Similar to AJAXDownload described in the  Wiki
<https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html>
  
except for the following modifications:

/**
     * Call this method to initiate the download.
     */
    public void initiate(AjaxRequestTarget target, String fileName,
InputStream is) {
        this.fileName = fileName;
        this.is = is;
        String url = getCallbackUrl().toString();

        if (addAntiCache) {
            url = url + (url.contains("?") ? "&" : "?");
            url = url + "antiCache=" + System.currentTimeMillis();
        }

        // the timeout is needed to let Wicket release the channel
        target.appendJavaScript("setTimeout(\"window.location.href='" + url
+ "'\", 100);");
    }


/**
     * Hook method providing the actual resource stream.
     */
    protected IResourceStream getResourceStream() {
        return new AbstractResourceStreamWriter() {

            public void write(Response output) {
                WebResponse response = (WebResponse)output;
                byte[] buffer = new byte[256];
                try {

                    int read;
                    while((read = is.read(buffer)) != -1) {
                        response.write(buffer, 0, read);
                        response.flush();
                    }

                    response.close();

                } catch (Exception e) {
                    response.write(e.toString().getBytes());
                    response.close();
                }
               
getComponent().getRequestCycle().setResponsePage(MCEVToolPage.class);
            }
        };
    }


Implementation:

final AjaxFileDownload fileDownload = new AjaxFileDownload();

AjaxLink fileDownloadLink = new AjaxLink("fileDownloadLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {
               StringBuilder fileNameBuilder = new StringBuilder();
               //Build filename
.....
.....

               StringBuilder dataBuilder = new StringBuilder();
               //builder data to be included in the file
......
......
               fileDownload.initiate(target, fileNameBuilder.toString(), new
ByteArrayInputStream(dataBuilder.toString().getBytes()));        
            }
 
} 
fileDownloadLink.add(fileDownload);
add(fileDownloadLink);


Now you can create a single instance of AjaxFileDownload and include that in
your Application file and request when ever needed.




--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/AJAX-update-and-file-download-on-Wicket-1-5-tp4095241p4657667.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to