Discussion:
calling downloadLink.onClick() directly
eaglei22
2014-03-25 21:56:12 UTC
Permalink
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-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
Martin Grigorov
2014-03-25 22:00:43 UTC
Permalink
Hi,

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

Martin Grigorov
Wicket Training and Consulting
Post by eaglei22
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
ᅵᅵ ࡱ ᅵ; ᅵᅵ ...` ᅵ ᅵ 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?
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
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?
--
http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
eaglei22
2014-03-26 14:25:19 UTC
Permalink
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-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
Martin Grigorov
2014-03-26 14:41:39 UTC
Permalink
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
Post by eaglei22
Thanks Martin,
I guess what I am more curious in, is how come when I call the
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..
--
http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125p4665133.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
eaglei22
2014-03-26 20:54:09 UTC
Permalink
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-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
Martin Grigorov
2014-03-27 09:16:33 UTC
Permalink
Check the Wiki page I gave you.
It uses target.appendJavaScript("location.href='"+ url + "';");

Martin Grigorov
Wicket Training and Consulting
Post by eaglei22
new RedirectPage(excelFile.getPath());
and,
target.appendJavaScript("location.hash='excelFile.getPath()'");
Can you give me a simple example?
--
http://apache-wicket.1842946.n4.nabble.com/calling-downloadLink-onClick-directly-tp4665125p4665136.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
Loading...