Download dialog does not appear

2

I am generating a xls in memory and it should appear for download , but at the end of the method the download dialog is not appearing.

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();
    }

}
    
asked by anonymous 17.11.2014 / 15:32

1 answer

1

By default, browsers do not persist on disk, files sent by Ajax. According to this SO response EN , the file even remains in memory, but does not provide the user with the option to save to disk.

To solve this problem, simply remove the Ajax from the button, or change a4j:commandButton to h:commandButton or any other component calling the action using submit of the form. With this the user will not change from View and will see the file download dialog.

    
17.11.2014 / 23:34