2
I’m generating a xls
in memory and it should appear to download, but at the end of the method the dialogue of download.
xhtml
<a4j:commandButton styleClass="Button" value="Download"
execute="@form" render="@form"
action="#{lotesEnvBean.ImprimirLotesEnviados()}" >
</a4j:commandButton>
Bean
public void ImprimirLotesEnviados() {
try {
SimpleDateFormat simpledate = new SimpleDateFormat("ddMMyyyyHHMMSS");
java.util.Date dateFile = new java.util.Date();
//FileOutputStream outFile = new FileOutputStream (new File("/home/joao/app/tmp/envLote"+simpledate.format(dateFile)+".xls"));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Lote Enviados");
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
HSSFCellStyle my_style_0 = workbook.createCellStyle();
my_style_0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
my_style_0.setFont(font);
my_style_0.setBorderLeft( (short) 100);
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID Familia");
header.getCell(0).setCellStyle(my_style_0);
header.createCell(1).setCellValue("Famailia");
header.getCell(1).setCellStyle(my_style_0);
header.createCell(2).setCellValue("ID Produto");
header.getCell(2).setCellStyle(my_style_0);
header.createCell(3).setCellValue("Produto");
header.getCell(3).setCellStyle(my_style_0);
Integer count=1;
for (LoteEnvDetalheEntity dto :this.lotesEnvDetalhe ) {
Row dataRow = sheet.createRow(count);
dataRow.createCell(0).setCellValue(dto.getSeqfamilia());
dataRow.createCell(1).setCellValue(dto.getFamilia());
dataRow.createCell(2).setCellValue(dto.getSeqproduto());
dataRow.createCell(3).setCellValue(dto.getProduto());
count++;
}
//workbook.write(outFile);
//outFile.close();
System.out.println("Excel written successfully..");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/download");
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"lotes_enviados_" + simpledate.format(dateFile) + ".xls");
System.out.println("attachment; filename=lotes_enviados_" + simpledate.format(dateFile) + ".xls");
OutputStream output = ec.getResponseOutputStream();
output.write(workbook.getBytes());
output.flush();
output.close();
fc.responseComplete();
}catch (Exception e) {
e.printStackTrace();
}
}
It is okay that it does not show the download window, as the browser may be set to download the file automatically. Please complete your question by describing what behavior is currently occurring. An error page? Blank page? Nothing? Already tracked the call through your browser’s Developer Tool to verify the HTTP code returned by the request generated by the button?
– utluiz
As far as I’m concerned, the problem lies in that line:
ec.setResponseContentType("application/download");
. Would not beapplication/vnd.ms-excel
, since you are making the user download a file with the extension "xls"?– Rodrigo Rigotti
Is it possible to download with AJAX request? I’ve had problems with this and ended up switching to a normal button, which gives Submit in the form.
– Wakim
@utluiz Nothing is happening, I used the developer tool, it arrives from the post, but it does not give anything. @Rodrigorigotti will test this, but I’ve seen people say that the
("application/download")
is a Generico. @Wakim I found people talking about it also I will test here.– João
@Wakim traded the button for one of the facelets and function, really was the component of the richfaces that was giving the problem.
– João
@Wakim Your question is very important. "Killed on purpose", as they say. It is not possible to download through Ajax, it needs to be a normal request. With JSF this is sometimes hazy because an Ajax call can sometimes be "transformed" (redirected, actually) into a normal request depending on the implementation.
– utluiz