You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by eaglei22 <jc...@gmail.com> on 2014/03/25 22:56:12 UTC

calling downloadLink.onClick() directly

Hello, I currently am using a downloadLink to export a csv file to the user.
What I am currently doing is using a worker thread to handle the creation of
the csv file while an AjaxSelfUpdatingTimerBehavior updates the GUI of its
progress.. Then at completion the intent is to call downloadLink.onClick()
to invoke the behind the scenes work and get the user the dialog box
automatically to download the file.  I get everything running fine until
downloadLink.onClick() is called I get an ajax error: 

"Wicket.Ajax.Call.failure: Error while parsing response: Error: Invalid XML:
��ࡱ�;��    ...`��Page 1�����=Bems_ID
First_NameMiddle_Name    Last_NameUser_Status"

it looks like the csv file is being outputted to the ajax debugger. 

What is the best way to invoke the onClick method automatically of the
DownloadLink class? 

Here is a snippet of what I am doing:

	private void setupDownloadLink()
	{
  	   IModel excelFileModel = new AbstractReadOnlyModel()
 	   {
		      public Object getObject() 
		      { 
		    	 if(excelFile == null)
		    	 {
		    		 excelFile = new File("Error getting File");
		    	 }
		        return excelFile;
		      }
		   };
		  
  	     auditDownloadlink = new DownloadLink("auditDownloadlink",
excelFileModel)
  	     { 			
  	    	public void onClick()
			{
  	    		downLoadLinkClicked = true;
				if (newSearchWasInitialized)
				{
					newSearchWasInitialized = false;
					if (!extractionIsRunning)
					{
						worker = new CreateExcelExtractionWorker();
						thread = new Thread(worker);
						thread.start();
						extractionIsRunning = true;
                        excelFile = null;
						return;
					}
				}				 
              
				if(!newSearchWasInitialized)
				{
					if (!extractionIsRunning)
					{
                      super.onClick();
					}
				}
			}
  	     };
  	     
  	     auditDownloadlink.add(new Label("excelLabel", "Excel"));
   	     auditDownloadlink.setOutputMarkupPlaceholderTag(true);
   	     auditDownloadlink.setDeleteAfterDownload(true);
   	     auditDownloadlink.setCacheDuration(Duration.ONE_SECOND);
   	     auditDownloadlink.setVisible(false);
   	     
   	     
   	  findUserForm.add(auditDownloadlink);
	}

Here basically when the user selects the button, it starts the worker
thread.. Then when completed, the AjaxSelfUpdatingTimerBehavior will call
the onClick() method, where in turn after each check is made then
super.onClick() is called to process the downloading of the file as normal:

			public void onPostProcessTarget(AjaxRequestTarget target)
			{
				System.out.println("isRunning");
				if(!extractionIsRunning)
				{
					if(downLoadLinkClicked)
					{
					  System.out.println("refreshed"); 
					  progressModel.setObject("Extraction to Excel file complete. Click            
Download");
					  auditDownloadlink.addOrReplace(new Label("excelLabel", "Download"));
					  target.add(findUserForm);
					  target.add(progress);
					  stop(target);
					  System.out.println("isStopped");
					  downLoadLinkClicked = false;
					  
                      auditDownloadlink.onClick();
					}
				}
			}
		};
		progress.add(progressUpdateBehavior);
		progress.setOutputMarkupId(true);

Do I have to attach it back to the requestCycle? 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125.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: calling downloadLink.onClick() directly

Posted by Martin Grigorov <mg...@apache.org>.
Check the Wiki page I gave you.
It uses target.appendJavaScript("location.href='"+ url + "';");

Martin Grigorov
Wicket Training and Consulting


On Wed, Mar 26, 2014 at 9:54 PM, eaglei22 <jc...@gmail.com> wrote:

> I am not familar on how to perform a redirect from AJAX. I tried:
>
>   new RedirectPage(excelFile.getPath());
>
>     and,
>
>   target.appendJavaScript("location.hash='excelFile.getPath()'");
>
> Can you give me a simple example?
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125p4665136.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: calling downloadLink.onClick() directly

Posted by eaglei22 <jc...@gmail.com>.
I am not familar on how to perform a redirect from AJAX. I tried:

  new RedirectPage(excelFile.getPath());

    and,

  target.appendJavaScript("location.hash='excelFile.getPath()'");

Can you give me a simple example?

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125p4665136.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: calling downloadLink.onClick() directly

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

You should not write the binary response in the Ajax response.
The ajax response should return a <redirect> that will point to a url where
the binary data will be written back.

Martin Grigorov
Wicket Training and Consulting


On Wed, Mar 26, 2014 at 4:25 PM, eaglei22 <jc...@gmail.com> wrote:

> Thanks Martin,
>
> I guess what I am more curious in, is how come when I call the
> downloadLink.onClick(), or do:
>
>
>         public void onPostProcessTarget(AjaxRequestTarget target)
>         {
>            final File file = excelFile;
>            IResourceStream resourceStream = new FileResourceStream(
>                                                     new
> org.apache.wicket.util.file.File(file));
>
>            ResourceStreamRequestHandler handler = new
> ResourceStreamRequestHandler(resourceStream,  file.getName());
>            handler.setContentDisposition(ContentDisposition.ATTACHMENT);
>
> getPage().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
>
>
> I get the ajax error as shown above thread. It's only on onClick() call, or
> when
> getPage().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
> is called..
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125p4665133.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: calling downloadLink.onClick() directly

Posted by eaglei22 <jc...@gmail.com>.
Thanks Martin,

I guess what I am more curious in, is how come when I call the
downloadLink.onClick(), or do:


	public void onPostProcessTarget(AjaxRequestTarget target)
	{
	   final File file = excelFile;
	   IResourceStream resourceStream = new FileResourceStream(
					            new org.apache.wicket.util.file.File(file));
					    
	   ResourceStreamRequestHandler handler = new
ResourceStreamRequestHandler(resourceStream,  file.getName());
	   handler.setContentDisposition(ContentDisposition.ATTACHMENT);		
	  getPage().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);


I get the ajax error as shown above thread. It's only on onClick() call, or
when 
getPage().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
is called..

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125p4665133.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: calling downloadLink.onClick() directly

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Check
https://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+file+download+in+one+blow

Martin Grigorov
Wicket Training and Consulting


On Tue, Mar 25, 2014 at 11:56 PM, eaglei22 <jc...@gmail.com> wrote:

> Hello, I currently am using a downloadLink to export a csv file to the
> user.
> What I am currently doing is using a worker thread to handle the creation
> of
> the csv file while an AjaxSelfUpdatingTimerBehavior updates the GUI of its
> progress.. Then at completion the intent is to call downloadLink.onClick()
> to invoke the behind the scenes work and get the user the dialog box
> automatically to download the file.  I get everything running fine until
> downloadLink.onClick() is called I get an ajax error:
>
> "Wicket.Ajax.Call.failure: Error while parsing response: Error: Invalid
> XML:
> �� ࡱ �; ��        ...`  � �  Page 1�   �        �� �= Bems_ID
> First_Name Middle_Name    Last_Name User_Status"
>
> it looks like the csv file is being outputted to the ajax debugger.
>
> What is the best way to invoke the onClick method automatically of the
> DownloadLink class?
>
> Here is a snippet of what I am doing:
>
>         private void setupDownloadLink()
>         {
>            IModel excelFileModel = new AbstractReadOnlyModel()
>            {
>                       public Object getObject()
>                       {
>                          if(excelFile == null)
>                          {
>                                  excelFile = new File("Error getting
> File");
>                          }
>                         return excelFile;
>                       }
>                    };
>
>              auditDownloadlink = new DownloadLink("auditDownloadlink",
> excelFileModel)
>              {
>                 public void onClick()
>                         {
>                         downLoadLinkClicked = true;
>                                 if (newSearchWasInitialized)
>                                 {
>                                         newSearchWasInitialized = false;
>                                         if (!extractionIsRunning)
>                                         {
>                                                 worker = new
> CreateExcelExtractionWorker();
>                                                 thread = new
> Thread(worker);
>                                                 thread.start();
>                                                 extractionIsRunning = true;
>                         excelFile = null;
>                                                 return;
>                                         }
>                                 }
>
>                                 if(!newSearchWasInitialized)
>                                 {
>                                         if (!extractionIsRunning)
>                                         {
>                       super.onClick();
>                                         }
>                                 }
>                         }
>              };
>
>              auditDownloadlink.add(new Label("excelLabel", "Excel"));
>              auditDownloadlink.setOutputMarkupPlaceholderTag(true);
>              auditDownloadlink.setDeleteAfterDownload(true);
>              auditDownloadlink.setCacheDuration(Duration.ONE_SECOND);
>              auditDownloadlink.setVisible(false);
>
>
>           findUserForm.add(auditDownloadlink);
>         }
>
> Here basically when the user selects the button, it starts the worker
> thread.. Then when completed, the AjaxSelfUpdatingTimerBehavior will call
> the onClick() method, where in turn after each check is made then
> super.onClick() is called to process the downloading of the file as normal:
>
>                         public void onPostProcessTarget(AjaxRequestTarget
> target)
>                         {
>                                 System.out.println("isRunning");
>                                 if(!extractionIsRunning)
>                                 {
>                                         if(downLoadLinkClicked)
>                                         {
>                                           System.out.println("refreshed");
>
> progressModel.setObject("Extraction to Excel file complete. Click
> Download");
>
> auditDownloadlink.addOrReplace(new Label("excelLabel", "Download"));
>                                           target.add(findUserForm);
>                                           target.add(progress);
>                                           stop(target);
>                                           System.out.println("isStopped");
>                                           downLoadLinkClicked = false;
>
>                       auditDownloadlink.onClick();
>                                         }
>                                 }
>                         }
>                 };
>                 progress.add(progressUpdateBehavior);
>                 progress.setOutputMarkupId(true);
>
> Do I have to attach it back to the requestCycle?
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125.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
>
>