You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ch...@t-systems.com on 2014/04/10 11:47:41 UTC

Download excel-file

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




AW: Download excel-file

Posted by Ch...@t-systems.com.
I created the class AJAXDownload and copied your code. Then I added the AJAXDownload to my download button like this:
    @Override
    public void onSubmit(AjaxRequestTarget target, Form form) {
        target.add(feedback);
        final SlaReportModel slaReportModel = (SlaReportModel)form.getModelObject();
        if(!slaReportModel.isEmpty()){
            download = new AJAXDownload() {
                @Override
                protected IResourceStream getResourceStream() {
                    return new FileResourceStream(new org.apache.wicket.util.file.File(createFileForDownload(slaReportModel.getTxtDatumVon(),slaReportModel.getTxtDatumBis())));
                }

                @Override
                protected String getFileName(){
                    return filename;
                }
            };
            this.add(download);
            download.initiate(target);
        }else{
            error("Sie m?ssen den Zeitraum angeben!");
        }

    }

It works fine :) thanks for your help.

Mit freundlichen Grüßen
Christoph Manig
Systems Engineer

T-Systems International GmbH
Systems Integration - SC Travel, Transport & Logistics
Hoyerswerdaer Str. 18
01099 Dresden 
tel.:	+49 (0) 351 / 8152 - 188
fax:	+49 (0) 351 / 8152 - 209
email:  Christoph.Manig@t-systems.com

T-SYSTEMS INTERNATIONAL GMBH
Aufsichtsrat: Thomas Dannenfeldt (Vorsitzender)
Geschäftsführung: Reinhard Clemens (Vorsitzender), Dr. Ferri Abolhassan, Thilo Kusch, Dr. Markus Müller, Georg Pepping, Hagen Rickmann
Handelsregister: Amtsgericht Frankfurt am Main HRB 55933
Sitz der Gesellschaft: Frankfurt am Main
WEEE-Reg.-Nr. DE50335567


-----Ursprüngliche Nachricht-----
Von: Ernesto Reinaldo Barreiro [mailto:reiern70@gmail.com] 
Gesendet: Donnerstag, 10. April 2014 12:08
An: users@wicket.apache.org
Betreff: Re: Download excel-file

Just follow the wiki: that's why it was created ;-)


On Thu, Apr 10, 2014 at 12:01 PM, <Ch...@t-systems.com> wrote:

> So I have to add the AjaxDownload to my form which add my DownloadButton?
> Like this:
>
> AJAXDownload download = new AJAXDownload() {
>     @Override
>     protected IResourceStream getResourceStream()
>     {
>         return createResourceStream(item.getModelObject());
>     }
> };
>
> Form<SlaReportModel> reportForm = new
> Form<SlaReportModel>("reportForm",new
> CompoundPropertyModel<SlaReportModel>(new SlaReportModel())); 
> reportForm.add(download); reportForm.add(new 
> CustomDownloadButton("submit", reportForm, feedback, "SLA_Report"));
>
>
> Mit freundlichen Grüßen
> Christoph Manig
>
> -----Ursprüngliche Nachricht-----
> Von: Ernesto Reinaldo Barreiro [mailto:reiern70@gmail.com]
> Gesendet: Donnerstag, 10. April 2014 11:53
> An: users@wicket.apache.org
> Betreff: Re: Download excel-file
>
> You cannot stream a PDF back to client via AJAX. Have a look at.
>
>
> https://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+fil
> e+download+in+one+blow
>
>
> On Thu, Apr 10, 2014 at 11:47 AM, <Ch...@t-systems.com> wrote:
>
> > 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(),slaRepo
> rtModel.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().getReportAsB
> yteStream(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
> >
> >
> >
> >
>
>
> --
> Regards - Ernesto Reinaldo Barreiro
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


--
Regards - Ernesto Reinaldo Barreiro

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


Re: Download excel-file

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Just follow the wiki: that's why it was created ;-)


On Thu, Apr 10, 2014 at 12:01 PM, <Ch...@t-systems.com> wrote:

> So I have to add the AjaxDownload to my form which add my DownloadButton?
> Like this:
>
> AJAXDownload download = new AJAXDownload()
> {
>     @Override
>     protected IResourceStream getResourceStream()
>     {
>         return createResourceStream(item.getModelObject());
>     }
> };
>
> Form<SlaReportModel> reportForm = new
> Form<SlaReportModel>("reportForm",new
> CompoundPropertyModel<SlaReportModel>(new SlaReportModel()));
> reportForm.add(download);
> reportForm.add(new CustomDownloadButton("submit", reportForm, feedback,
> "SLA_Report"));
>
>
> Mit freundlichen Grüßen
> Christoph Manig
>
> -----Ursprüngliche Nachricht-----
> Von: Ernesto Reinaldo Barreiro [mailto:reiern70@gmail.com]
> Gesendet: Donnerstag, 10. April 2014 11:53
> An: users@wicket.apache.org
> Betreff: Re: Download excel-file
>
> You cannot stream a PDF back to client via AJAX. Have a look at.
>
>
> https://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+file+download+in+one+blow
>
>
> On Thu, Apr 10, 2014 at 11:47 AM, <Ch...@t-systems.com> wrote:
>
> > 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
> >
> >
> >
> >
>
>
> --
> Regards - Ernesto Reinaldo Barreiro
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Regards - Ernesto Reinaldo Barreiro

AW: Download excel-file

Posted by Ch...@t-systems.com.
So I have to add the AjaxDownload to my form which add my DownloadButton? Like this:

AJAXDownload download = new AJAXDownload()
{
    @Override
    protected IResourceStream getResourceStream()
    {
        return createResourceStream(item.getModelObject());
    }
};

Form<SlaReportModel> reportForm = new Form<SlaReportModel>("reportForm",new CompoundPropertyModel<SlaReportModel>(new SlaReportModel()));
reportForm.add(download);
reportForm.add(new CustomDownloadButton("submit", reportForm, feedback, "SLA_Report"));


Mit freundlichen Grüßen
Christoph Manig

-----Ursprüngliche Nachricht-----
Von: Ernesto Reinaldo Barreiro [mailto:reiern70@gmail.com] 
Gesendet: Donnerstag, 10. April 2014 11:53
An: users@wicket.apache.org
Betreff: Re: Download excel-file

You cannot stream a PDF back to client via AJAX. Have a look at.

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


On Thu, Apr 10, 2014 at 11:47 AM, <Ch...@t-systems.com> wrote:

> 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
>
>
>
>


--
Regards - Ernesto Reinaldo Barreiro

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


Re: Download excel-file

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
You cannot stream a PDF back to client via AJAX. Have a look at.

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


On Thu, Apr 10, 2014 at 11:47 AM, <Ch...@t-systems.com> wrote:

> 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
>
>
>
>


-- 
Regards - Ernesto Reinaldo Barreiro