Hello,
I want to download an excelfile, which will be created on the fly by clicking
on a button. So I have created my own downloadButton. The excel-file will be
created, it works fine but the download do not work. I have started my
application in development mode, but no exception will be thrown. But in the
wicket ajax panel I found this message: ERROR: Wicket.Ajax.Call.failure: Error
while parsing response: Could not find root <ajax-response> element
Here is the code of my button:
public class CustomDownloadButton extends AjaxFallbackButton {
private static final Logger log =
LoggerFactory.getLogger(CustomDownloadButton.class);
private CSSFeedbackPanel feedback;
private String filename;
public CustomDownloadButton(String id, Form<?> form, CSSFeedbackPanel
feedback, String filename) {
super(id, form);
this.feedback = feedback;
this.filename = filename;
}
@Override
public void onSubmit(AjaxRequestTarget target, Form form) {
target.add(feedback);
SlaReportModel slaReportModel = (SlaReportModel)form.getModelObject();
if(!slaReportModel.isEmpty()){
download(createFileForDownload(slaReportModel.getTxtDatumVon(),slaReportModel.getTxtDatumBis()));
}else{
error("Sie müssen den Zeitraum angeben!");
}
}
private File createFileForDownload(Date beginDate, Date endDate) {
log.debug("Creating file for download!");
File tempFile = null;
try
{
tempFile = File.createTempFile("SLA", ".xls");
InputStream data = new
ByteArrayInputStream(ConsoleDataHandlerImpl.getInstance().getReportAsByteStream(beginDate,endDate));
Files.writeTo(tempFile, data);
} catch (Exception e) {
log.debug(e.getStackTrace().toString());
}
log.debug("File " + tempFile.getName() + " was successfully created!");
return tempFile;
}
private void download(final File fileForDownload) {
log.debug("Preparing download file " + fileForDownload.getName());
System.out.println(fileForDownload.toString());
if (fileForDownload == null){
throw new IllegalStateException(getClass().getName() + " there is
no file, file is null!");
}
IResourceStream resourceStream = new FileResourceStream(new
org.apache.wicket.util.file.File(fileForDownload));
getRequestCycle().scheduleRequestHandlerAfterCurrent(new
ResourceStreamRequestHandler(resourceStream) {
@Override
public void respond(IRequestCycle requestCycle) {
super.respond(requestCycle);
//Files.remove(fileForDownload);
}
}.setFileName(this.filename));
//.setContentDisposition(ContentDisposition.ATTACHMENT)
//.setCacheDuration(org.apache.wicket.util.time.Duration.NONE));
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form)
{
// repaint the feedback panel so errors are shown
target.add(feedback);
}
}
Mit freundlichen Grüßen
Christoph Manig