You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by juhar <ju...@trimico.fi> on 2012/11/22 12:51:56 UTC

File download through form submit & feedback messages

Hi,

I have a page with a form on it. After the user has submitted the form
successfully (all the fields have validated ok), the server creates a
report-file based on the form values. The file is not created as physical
file on the server, but it is given as download to the user.

Now, if the user first fills the form incorrectly, there are validator
feedback messages shown. If the user then submits the form with correct
values, the problem is that previous feedback error messages are not
cleared. I can't also create a new feedback message saying something like
"The report was created successfully." The file download response prevents
any updating of the current page.

I tried submitting the form with ajax using AjaxButton, and was able to
clear the feedback messages, but unfortunately it would not work on IE8...
IE8 will warn you about the file with a pop-up, and after that the download
will just disappear. Also any of my attempts to manually clear the feedback
messages have failed.

What would be correct way of creating this? I'm using Wicket6. This is the
code I'm using currently (relevant bits only):

// In the Panel-constructor
form.add(new Button("createreportbutton") {
    @Override
    public void onSubmit() {
        createReport();
    }
});

// Generates the report, and response
private void createReport() {
    // Generate report with something like:
    reportCommand.generateReport(formValues);
    final byte[] reportData  = reportCommand.getReportData();
    final String contentType = reportCommand.getContentType();
    final String contentDisposition = reportCommand.getContentDisposition();

    // Give the report as download
    getRequestCycle().scheduleRequestHandlerAfterCurrent( new
IRequestHandler() {
        @Override
        public void detach(IRequestCycle reqCycle) {
        }

        @Override
        public void respond(IRequestCycle reqCycle) {
            WebResponse response = (WebResponse)reqCycle.getResponse();
            HttpServletResponse res =
(HttpServletResponse)response.getContainerResponse();
            res.setContentType(contentType);
            res.setHeader("Content-Disposition", contentDisposition);
            try {
                res.getOutputStream().write(reportData);
            } catch (IOException ex) {
                ApplicationLogger.error("Error writing report to HTTP
response", ex);
            }

        }
    });        
}

Thanks,
Juha



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/File-download-through-form-submit-feedback-messages-tp4654087.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: File download through form submit & feedback messages

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

On Thu, Nov 22, 2012 at 1:12 PM, juhar <ju...@trimico.fi> wrote:

> Thanks, I already tried that one, and it worked great on other browsers,
> except on IE8 :( With IE8, I would get a pop-up warning about a possibly
> harmful file. I guess IE8 does this with javascript initiated downloads.
> After accepting the file, the download just disappears into thin air.
>
> This has always been a limitation of that approach: it might not work for
some security settings of the browser.


> Large portion of our users are still using IE8, so I was wondering if
> there's a way to do this without ajax. Or some kind of hack for IE8 to make
> the ajax download succeed. Maybe this is more of an IE8 issue after all...
>

Probably the safest thing is to do it in two steps.

1- do form processing
2- if successful show a panel "click here to get the file" with a download
link (where you actually generate the file).

-- 
Regards - Ernesto Reinaldo Barreiro
Antilia Soft
http://antiliasoft.com/ <http://antiliasoft.com/antilia>

Re: File download through form submit & feedback messages

Posted by juhar <ju...@trimico.fi>.
Thanks, I already tried that one, and it worked great on other browsers,
except on IE8 :( With IE8, I would get a pop-up warning about a possibly
harmful file. I guess IE8 does this with javascript initiated downloads.
After accepting the file, the download just disappears into thin air. 

Large portion of our users are still using IE8, so I was wondering if
there's a way to do this without ajax. Or some kind of hack for IE8 to make
the ajax download succeed. Maybe this is more of an IE8 issue after all...

-juha    



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/File-download-through-form-submit-feedback-messages-tp4654087p4654089.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: File download through form submit & feedback messages

Posted by Bas Gooren <ba...@iswd.nl>.
Hi,

Have a look at 
https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html 
and try to make it work in wicket 6 (should be easy).

This allows you to do ajax updates (e.g. refresh the form) and then 
redirect the browser to the file.

Met vriendelijke groet,
Kind regards,

Bas Gooren

Op 22-11-2012 12:51, schreef juhar:
> Hi,
>
> I have a page with a form on it. After the user has submitted the form
> successfully (all the fields have validated ok), the server creates a
> report-file based on the form values. The file is not created as physical
> file on the server, but it is given as download to the user.
>
> Now, if the user first fills the form incorrectly, there are validator
> feedback messages shown. If the user then submits the form with correct
> values, the problem is that previous feedback error messages are not
> cleared. I can't also create a new feedback message saying something like
> "The report was created successfully." The file download response prevents
> any updating of the current page.
>
> I tried submitting the form with ajax using AjaxButton, and was able to
> clear the feedback messages, but unfortunately it would not work on IE8...
> IE8 will warn you about the file with a pop-up, and after that the download
> will just disappear. Also any of my attempts to manually clear the feedback
> messages have failed.
>
> What would be correct way of creating this? I'm using Wicket6. This is the
> code I'm using currently (relevant bits only):
>
> // In the Panel-constructor
> form.add(new Button("createreportbutton") {
>      @Override
>      public void onSubmit() {
>          createReport();
>      }
> });
>
> // Generates the report, and response
> private void createReport() {
>      // Generate report with something like:
>      reportCommand.generateReport(formValues);
>      final byte[] reportData  = reportCommand.getReportData();
>      final String contentType = reportCommand.getContentType();
>      final String contentDisposition = reportCommand.getContentDisposition();
>
>      // Give the report as download
>      getRequestCycle().scheduleRequestHandlerAfterCurrent( new
> IRequestHandler() {
>          @Override
>          public void detach(IRequestCycle reqCycle) {
>          }
>
>          @Override
>          public void respond(IRequestCycle reqCycle) {
>              WebResponse response = (WebResponse)reqCycle.getResponse();
>              HttpServletResponse res =
> (HttpServletResponse)response.getContainerResponse();
>              res.setContentType(contentType);
>              res.setHeader("Content-Disposition", contentDisposition);
>              try {
>                  res.getOutputStream().write(reportData);
>              } catch (IOException ex) {
>                  ApplicationLogger.error("Error writing report to HTTP
> response", ex);
>              }
>
>          }
>      });
> }
>
> Thanks,
> Juha
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/File-download-through-form-submit-feedback-messages-tp4654087.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: File download through form submit & feedback messages

Posted by Joachim Schrod <js...@acm.org>.
juhar wrote:
> Hi,
> 
> I have a page with a form on it. After the user has submitted the form
> successfully (all the fields have validated ok), the server creates a
> report-file based on the form values. The file is not created as physical
> file on the server, but it is given as download to the user.
> 
> Now, if the user first fills the form incorrectly, there are validator
> feedback messages shown. If the user then submits the form with correct
> values, the problem is that previous feedback error messages are not
> cleared. I can't also create a new feedback message saying something like
> "The report was created successfully." The file download response prevents
> any updating of the current page.
> 

Have you tried to use a resource link with
 a) an AJAX action to update your page
 b) a Call-Decorator that adds "return true" to the onclick-Event.
    That causes the genuine Resource-Link-Action to be done which
    initiate a download.
 c) Maybe also use target="_blank" in your Link-HTML. If there are
    errors in your download code, you can arrange to show them in a
    new situation-specific error page.

Something like:

final ResourceLink<Object> downloadPDF = new ResourceLink<Object>("downloadPDF", pdfResource);
downloadPDF.add(new AjaxEventBehavior ("onclick") {
    private static final long serialVersionUID = 1L;
    @Override protected void onEvent (AjaxRequestTarget target) {
	// update actions for page go here
    }
    @Override protected IAjaxCallDecorator getAjaxCallDecorator() {
	return new AjaxCallDecorator() {
	    private static final long serialVersionUID = 1L;
	    @Override public CharSequence decorateScript(CharSequence script) {
		return script+"return true;";
	    }
	};
    }
});

This is working 1.4 code (it has to run on JBoss 4, I can't use 1.5ff),
no problems with IE8. (pdfResource is a WebResource-subclass object that
actually generates a PDF document.) IAjaxCallDecorator is gone in Wicket 6,
but there is a replacement for it; that was mentioned on this list
recently.

	Joachim

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Joachim Schrod, Roedermark, Germany
Email: jschrod@acm.org


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