You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jordi Deu-Pons <jo...@jordeu.net> on 2011/11/22 12:13:32 UTC

AJAX update and file download on Wicket 1.5

Hi,

 I want to do an AJAX update and then start a file download.

 I've found this solution for Wicket 1.4.x
https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html

 This is not working on Wicket 1.5 because this deprecated code:

public void onRequest()
{
	getComponent().getRequestCycle().setRequestTarget(
		new ResourceStreamRequestTarget(getResourceStream(), getFileName()));
}


Which is the best way to do this in 1.5 ?

Thanks!.


-- 
a10! i fins aviat.
J:-Deu

Re: AJAX update and file download on Wicket 1.5

Posted by msalman <mo...@yahoo.com>.
Thanks for your response.  Can you please kindly provide an outline or a few
lines of code to get me started.  My particular concerns are:

1. How do I create an Ajax request and AjaxRequestTarget.  Will this be
another Ajaxbehaviour.  Sorry I should have learned this by now but...

2. How and when do I stop this polling and end the original onSubmit call.

Thanks.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by Sven Meier <sv...@meiers.net>.
Hi,

the browser sends in a separate request to fetch the resource. You 
cannot show any feedback from there.

You can add an error to the session, and let the web page poll with ajax 
requests for any problems.

Regards
Sven


On 06/13/2014 03:35 AM, msalman wrote:
> Hi,
>
> Suppose there is some problem or exception in the file download. Let's say
> in the following method:
>
> 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();
>                  }
>              }
>          };
>      }
>
>
> And I want to show the error in the feedback panel for the page.  How can I
> do that?  The AjaxRequestTarget is not available here.  I tried to do
> something like the following but it did not work:
>
> parentPage.getSession().error(error);
> 					
> WebApplication app = (WebApplication)getComponent().getApplication();
> AjaxRequestTarget target = app.newAjaxRequestTarget(parentPage);
> target.add(parentPage.getFeedback());
>
>
> I also tried saving the AjaxRequestTarget passed in the initiate method as a
> member variable of the class and use it later for posting the error. Well,
> for one it did not work.  For second, I got the non serializable error for
> the target in the log.
>
>
>
> Thanks
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/AJAX-update-and-file-download-on-Wicket-1-5-tp4095241p4666243.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by msalman <mo...@yahoo.com>.
Well, that is exactly what we are trying to avoid.  An error page is a must
fix bug.

Thanks.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by malebu <mi...@gmail.com>.
You can always redirect to an error page incase of exceptions / errors.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by msalman <mo...@yahoo.com>.
Hi,

Suppose there is some problem or exception in the file download. Let's say
in the following method:

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


And I want to show the error in the feedback panel for the page.  How can I
do that?  The AjaxRequestTarget is not available here.  I tried to do
something like the following but it did not work:

parentPage.getSession().error(error);
					
WebApplication app = (WebApplication)getComponent().getApplication();
AjaxRequestTarget target = app.newAjaxRequestTarget(parentPage);
target.add(parentPage.getFeedback());


I also tried saving the AjaxRequestTarget passed in the initiate method as a
member variable of the class and use it later for posting the error. Well,
for one it did not work.  For second, I got the non serializable error for
the target in the log.



Thanks

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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by malebu <mi...@gmail.com>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by Jordi Deu-Pons <jo...@jordeu.net>.
Ok,

 I'll test this solution and update the wiki.

 Thanks.


On Tue, Nov 22, 2011 at 1:17 PM, Ernesto Reinaldo Barreiro <
reiern70@gmail.com> wrote:

> Jordi,
>
> Can you please update the wiki page with Martins suggestion for 1.5.x?
>
> Regards,
>
> Ernesto
>
> On Tue, Nov 22, 2011 at 1:11 PM, Martin Grigorov <mg...@apache.org>
> wrote:
> > On Tue, Nov 22, 2011 at 1:13 PM, Jordi Deu-Pons <jo...@jordeu.net>
> wrote:
> >> Hi,
> >>
> >>  I want to do an AJAX update and then start a file download.
> >>
> >>  I've found this solution for Wicket 1.4.x
> >>
> https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html
> >>
> >>  This is not working on Wicket 1.5 because this deprecated code:
> >>
> >> public void onRequest()
> >> {
> >>        getComponent().getRequestCycle().setRequestTarget(
> >>                new ResourceStreamRequestTarget(getResourceStream(),
> getFileName()));
> >
> >    getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(
> >                 new ResourceStreamRequestHandler(getResourceStream(),
> > getFileName()));
> >
> >
> >> }
> >>
> >>
> >> Which is the best way to do this in 1.5 ?
> >>
> >> Thanks!.
> >>
> >>
> >> --
> >> a10! i fins aviat.
> >> J:-Deu
> >>
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
a10! i fins aviat.
J:-Deu

Re: AJAX update and file download on Wicket 1.5

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Jordi,

Can you please update the wiki page with Martins suggestion for 1.5.x?

Regards,

Ernesto

On Tue, Nov 22, 2011 at 1:11 PM, Martin Grigorov <mg...@apache.org> wrote:
> On Tue, Nov 22, 2011 at 1:13 PM, Jordi Deu-Pons <jo...@jordeu.net> wrote:
>> Hi,
>>
>>  I want to do an AJAX update and then start a file download.
>>
>>  I've found this solution for Wicket 1.4.x
>> https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html
>>
>>  This is not working on Wicket 1.5 because this deprecated code:
>>
>> public void onRequest()
>> {
>>        getComponent().getRequestCycle().setRequestTarget(
>>                new ResourceStreamRequestTarget(getResourceStream(), getFileName()));
>
>    getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(
>                 new ResourceStreamRequestHandler(getResourceStream(),
> getFileName()));
>
>
>> }
>>
>>
>> Which is the best way to do this in 1.5 ?
>>
>> Thanks!.
>>
>>
>> --
>> a10! i fins aviat.
>> J:-Deu
>>
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: AJAX update and file download on Wicket 1.5

Posted by Martin Grigorov <mg...@apache.org>.
On Tue, Nov 22, 2011 at 1:13 PM, Jordi Deu-Pons <jo...@jordeu.net> wrote:
> Hi,
>
>  I want to do an AJAX update and then start a file download.
>
>  I've found this solution for Wicket 1.4.x
> https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html
>
>  This is not working on Wicket 1.5 because this deprecated code:
>
> public void onRequest()
> {
>        getComponent().getRequestCycle().setRequestTarget(
>                new ResourceStreamRequestTarget(getResourceStream(), getFileName()));

   getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(
                new ResourceStreamRequestHandler(getResourceStream(),
getFileName()));


> }
>
>
> Which is the best way to do this in 1.5 ?
>
> Thanks!.
>
>
> --
> a10! i fins aviat.
> J:-Deu
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org