I’m trying to stream the contents of a BLOB column in a database to the browser as a file. The contents of the BLOB is a gzipped XML file. This works in IE7 but not in Firefox 3.5. Here is how it’s implemented. I created a DownloadResource class:
public class DownloadResource extends StreamResource {
private final String filename;
public DownloadResource(byte[] fileBytes, String fileName, Application application) {
super(new ByteStreamResource(fileBytes), fileName, application);
this.filename = fileName;
}
@Override
public DownloadStream getStream() {
DownloadStream stream = new DownloadStream(getStreamSource().getStream(), "text/xml", filename);
stream.setParameter("Content-Disposition", "attachment;filename=" + filename);
stream.setParameter("Content-Encoding", "gzip");
stream.setCacheTime(5000);
return stream;
}
private static class ByteStreamResource implements StreamResource.StreamSource {
private final InputStream inputStream;
public ByteStreamResource(byte[] fileBytes) {
inputStream = new ByteArrayInputStream(fileBytes);
}
public InputStream getStream() {
return inputStream;
}
}
}
Then a button that lives in a table to stream the contents:
addGeneratedColumn("xml", new ColumnGenerator() {
public Component generateCell(Table source, Object itemId, Object columnId) {
final Object id = getContainerProperty(itemId, "id").getValue();
final byte[] xml = (byte[]
) getContainerProperty(itemId, "xml").getValue();
Button dlButton = new Button();
dlButton.setStyleName(Button.STYLE_LINK);
dlButton.setIcon(new ThemeResource("icons/16/document.png"));
dlButton.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
DownloadResource dlResource = new DownloadResource(xml, (id + ".xml"), app);
app.getMainWindow().open(dlResource, "_blank");
}
});
return dlButton;
}
});