By the way, i changed it a bit and now i repaint the link instead of repainting
the label, so the label isn't needed.
private final DocumentResourceListener documentResourceListener;
private final MyPdfResource pdf;
..........................
final AjaxFallbackLink link = new AjaxFallbackLink(componentId)
{
private static final long serialVersionUID =
8947164550163497764L;
@Override
public void onClick(final AjaxRequestTarget target) {
if (target != null) {
target.addComponent(this);
}
final BillItem item = (BillItem) model.getObject();
try {
pdf.setAttachmentname(item.getBillNumber() +
".pdf");
pdf.setContent(PdfHelper.generatePdf(item));
final String url =
BillDataviewPanel.this.documentResourceListener.getURL().toString();
target.appendJavascript("window.location.href='" +
url + "';");
} catch (final Exception e) {
Logger.error(e);
}
}
};
..........................
this.documentResourceListener = new
DocumentResourceListener("listener", this.pdf);
this.add(this.documentResourceListener);
And i changed MyPdfResource tob e more flexible;
public class MyPdfResource extends DynamicWebResource {
private byte[] content = null;
private String attachmentname = null;
public String getAttachmentname() {
return this.attachmentname;
}
@Override
protected ResourceState getResourceState() {
return new ResourceState() {
@Override
public String getContentType() {
return "application/pdf";
}
@Override
public byte[] getData() {
try {
return MyPdfResource.this.content;
} catch (final Exception e) {
return null;
}
}
};
}
public void setAttachmentname(final String name) {
this.attachmentname = name;
}
public void setContent(final byte[] content) {
this.content = content;
}
@Override
protected void setHeaders(final WebResponse response) {
super.setHeaders(response);
response.setAttachmentHeader(this.attachmentname);
}
}
Mit freundlichen Grüßen
Christian Giambalvo
--
Fachinformatiker für Anwendungsentwicklung
EXCELSIS Informationssysteme GmbH
Wilhelmsplatz 8 - 70182 Stuttgart
Mobile +49 176 196 32 406
Office +49 711 6 20 30 406
[email protected]
www.excelsisnet.com
www.twitter.com/excelsis_info
Sitz Stuttgart
Amtsgericht Stuttgart, HRB 21104
Geschäftsführer: Christian Sauter, Dr. Nils Herda, Frank Wolf
-----Ursprüngliche Nachricht-----
Von: Ernesto Reinaldo Barreiro [mailto:[email protected]]
Gesendet: Dienstag, 6. Oktober 2009 13:18
An: [email protected]
Betreff: Re: Send file to client via Ajax
Done!
http://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+file+download+in+one+blow
<http://cwiki.apache.org/confluence/display/WICKET/AJAX+update+and+file+download+in+one+blow>
Ernesto
On Tue, Oct 6, 2009 at 12:15 PM, Giambalvo, Christian <
[email protected]> wrote:
> Yes, please add it.
>
> Mit freundlichen Grüßen
> Christian Giambalvo
> --
> Fachinformatiker für Anwendungsentwicklung
>
> EXCELSIS Informationssysteme GmbH
> Wilhelmsplatz 8 - 70182 Stuttgart
> Mobile +49 176 196 32 406
> Office +49 711 6 20 30 406
> [email protected]
> www.excelsisnet.com
> www.twitter.com/excelsis_info
>
> Sitz Stuttgart
> Amtsgericht Stuttgart, HRB 21104
> Geschäftsführer: Christian Sauter, Dr. Nils Herda, Frank Wolf
>
>
> -----Ursprüngliche Nachricht-----
> Von: Ernesto Reinaldo Barreiro [mailto:[email protected]]
> Gesendet: Dienstag, 6. Oktober 2009 11:48
> An: [email protected]
> Betreff: Re: Send file to client via Ajax
>
> Nice to hear it was of some help! I have learned a bit too while doing
> it:-)
> Cheers,
>
> Ernesto
>
> P.S. Shall I add this to a Wiki page?... when I find the time;-)
>
> On Tue, Oct 6, 2009 at 11:39 AM, Giambalvo, Christian <
> [email protected]> wrote:
>
> > Works like a charm.
> > I think i've learned something about resource download via ajax.
> > Thanks for hat.
> >
> >
> > Mit freundlichen Grüßen
> > Christian Giambalvo
> > --
> > Fachinformatiker für Anwendungsentwicklung
> >
> > EXCELSIS Informationssysteme GmbH
> > Wilhelmsplatz 8 - 70182 Stuttgart
> > Mobile +49 176 196 32 406
> > Office +49 711 6 20 30 406
> > [email protected]
> > www.excelsisnet.com
> > www.twitter.com/excelsis_info
> >
> > Sitz Stuttgart
> > Amtsgericht Stuttgart, HRB 21104
> > Geschäftsführer: Christian Sauter, Dr. Nils Herda, Frank Wolf
> >
> > -----Ursprüngliche Nachricht-----
> > Von: Ernesto Reinaldo Barreiro [mailto:[email protected]]
> > Gesendet: Dienstag, 6. Oktober 2009 11:31
> > An: [email protected]
> > Betreff: Re: Send file to client via Ajax
> >
> > Just sent it on my last e-mail! Here it is again!
> > import org.apache.wicket.IResourceListener;
> > import org.apache.wicket.markup.html.WebMarkupContainer;
> >
> > /**
> > * @author Ernesto Reinaldo Barreiro ([email protected])
> > *
> > */
> > public class DocumentResourceListener extends WebMarkupContainer
> implements
> > IResourceListener
> > {
> > private static final long serialVersionUID = 1L;
> > private IResourceListener resourceListener;
> >
> > /**
> > * Constructor receiving an IResourceListener..
> > *
> > * @param id
> > * @param resourceListener
> > */
> > public DocumentResourceListener(final String id, IResourceListener
> > resourceListener)
> > {
> > super(id);
> > this.resourceListener = resourceListener;
> > }
> >
> > /**
> > * Gets the url to use for this link.
> > *
> > * @return The URL that this link links to
> > */
> > protected CharSequence getURL()
> > {
> > return urlFor(IResourceListener.INTERFACE);
> > }
> >
> >
> > @Override
> > protected boolean getStatelessHint()
> > {
> > return false;
> > }
> > public void onResourceRequested() {
> > this.resourceListener.onResourceRequested();
> > }
> > }
> >
> > This is just a trick to have component that generates file contents on
> the
> > same page! I think you could used mounted resources as well, or just a
> > dedicated servlet if you want to get file generation out of wicket!
> >
> > Best,
> >
> > Ernesto
> >
> > On Tue, Oct 6, 2009 at 11:24 AM, Giambalvo, Christian <
> > [email protected]> wrote:
> >
> > > Yes it would be usefull.
> > > But where does DocumentResourceListener came from?
> > > I'm using wicket 1.3.6 and DocumentResourceListener is not available.
> > >
> > > Thanks
> > >
> > > Mit freundlichen Grüßen
> > > Christian Giambalvo
> > > --
> > > Fachinformatiker für Anwendungsentwicklung
> > >
> > > EXCELSIS Informationssysteme GmbH
> > > Wilhelmsplatz 8 - 70182 Stuttgart
> > > Mobile +49 176 196 32 406
> > > Office +49 711 6 20 30 406
> > > [email protected]
> > > www.excelsisnet.com
> > >
> > > Sitz Stuttgart
> > > Amtsgericht Stuttgart, HRB 21104
> > > Geschäftsführer: Christian Sauter, Dr. Nils Herda, Frank Wolf
> > >
> > >
> > > -----Ursprüngliche Nachricht-----
> > > Von: Ernesto Reinaldo Barreiro [mailto:[email protected]]
> > > Gesendet: Dienstag, 6. Oktober 2009 09:54
> > > An: [email protected]
> > > Betreff: Re: Send file to client via Ajax
> > >
> > > Hi Cristian,
> > > I've got it working!
> > >
> > > Just place all the following files at the same package and you will get
> a
> > > working example (I tested them with FF35 and IE7).
> > >
> > > import org.apache.wicket.ajax.AjaxRequestTarget;
> > > import org.apache.wicket.ajax.markup.html.AjaxLink;
> > > import org.apache.wicket.markup.html.WebPage;
> > > import org.apache.wicket.markup.html.basic.Label;
> > > import org.apache.wicket.model.AbstractReadOnlyModel;
> > >
> > > /**
> > > * @author Ernesto Reinaldo Barreiro ([email protected])
> > > *
> > > */
> > > public class TestPage extends WebPage{
> > >
> > > private Label text;
> > > private String labelText = "Hi!";
> > > private DocumentResourceListener documentResourceListener;
> > >
> > > /**
> > > *
> > > */
> > > public TestPage() {
> > > AjaxLink<Void> link = new AjaxLink<Void>("link") {
> > > private static final long serialVersionUID = 1L;
> > >
> > > @Override
> > > public void onClick(AjaxRequestTarget target) {
> > > TestPage.this.labelText = "Hi! and donwload image!";
> > > if(target!= null) {
> > > target.addComponent(TestPage.this.text);
> > > String url = documentResourceListener.getURL().toString();
> > >
> target.appendJavascript(";alert('Hi');window.location.href='"+url+"';");
> > > }
> > > }
> > > };
> > > add(link);
> > > text = new Label("text", new AbstractReadOnlyModel<String>() {
> > > private static final long serialVersionUID = 1L;
> > >
> > > @Override
> > > public String getObject() {
> > > return TestPage.this.labelText;
> > > }
> > > });
> > > text.setOutputMarkupId(true);
> > > add(text);
> > > documentResourceListener = new DocumentResourceListener("listener",
> new
> > > MyPdfResource());
> > > add(documentResourceListener);
> > > }
> > >
> > > }
> > >
> > > ---------------------------
> > >
> > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > > <html xmlns:wicket="org.apache.wicket">
> > > <head>
> > > </head>
> > > <body>
> > > <a wicket:id="link">Click Me</a>
> > > <span wicket:id="text"></span>
> > > <div wicket:id="listener"></div>
> > > </body>
> > > </html>
> > >
> > > ---------------------------------
> > >
> > > import java.io.ByteArrayOutputStream;
> > > import java.io.IOException;
> > > import java.io.InputStream;
> > > import java.io.OutputStream;
> > >
> > > import org.apache.wicket.markup.html.DynamicWebResource;
> > > import org.apache.wicket.protocol.http.WebResponse;
> > >
> > > /**
> > > * @author Ernesto Reinaldo Barreiro ([email protected])
> > > *
> > > */
> > > public class MyPdfResource extends DynamicWebResource {
> > >
> > > private static final long serialVersionUID = 1L;
> > >
> > > static int BUFFER_SIZE = 10*1024;
> > > /**
> > > *
> > > */
> > > public MyPdfResource() {
> > > }
> > >
> > > /* (non-Javadoc)
> > > * @see
> > org.apache.wicket.markup.html.DynamicWebResource#getResourceState()
> > > */
> > > @Override
> > > protected ResourceState getResourceState() {
> > > return new ResourceState() {
> > > @Override
> > > public String getContentType() {
> > > return "application/pdf";
> > > }
> > > @Override
> > > public byte[] getData() {
> > > try {
> > > return
> > bytes(MyPdfResource.class.getResourceAsStream("jta-1_1-spec.pdf"));
> > > } catch (Exception e) {
> > > return null;
> > > }
> > > }
> > > };
> > > }
> > > public static byte[] bytes(InputStream is) throws IOException {
> > > ByteArrayOutputStream out = new ByteArrayOutputStream();
> > > copy(is, out);
> > > return out.toByteArray();
> > > }
> > > @Override
> > > protected void setHeaders(WebResponse response) {
> > > super.setHeaders(response);
> > > response.setAttachmentHeader("jta-1_1-spec.pdf");
> > > }
> > > public static void copy(InputStream is, OutputStream os) throws
> > > IOException
> > > {
> > > byte[] buf = new byte[BUFFER_SIZE];
> > > while (true) {
> > > int tam = is.read(buf);
> > > if (tam == -1) {
> > > return;
> > > }
> > > os.write(buf, 0, tam);
> > > }
> > > }
> > > }
> > >
> > > Just replace "jta-1_1-spec.pdf" with your own PDF file on MyPdfResource
> > and
> > > the exampel should work. An important bit here if
> > >
> > > @Override
> > > protected void setHeaders(WebResponse response) {
> > > ....
> > > response.setAttachmentHeader("jta-1_1-spec.pdf");
> > > }
> > >
> > > which makes the file to treated as an attachment instead of replacing
> the
> > > current page.
> > >
> > > Best,
> > >
> > > Ernesto
> > >
> > > P.S. Would it useful to add this to Wicket's wiki?
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [email protected]
> > > For additional commands, e-mail: [email protected]
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]