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
christian.giamba...@excelsisnet.com
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:reier...@gmail.com] 
Gesendet: Dienstag, 6. Oktober 2009 09:54
An: users@wicket.apache.org
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 (reier...@gmail.com)
 *
 */
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 (reier...@gmail.com)
 *
 */
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: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to