Download dialog does not appear

Asked

Viewed 111 times

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?

  • As far as I’m concerned, the problem lies in that line: ec.setResponseContentType("application/download");. Would not be application/vnd.ms-excel, since you are making the user download a file with the extension "xls"?

  • 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.

  • @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.

  • @Wakim traded the button for one of the facelets and function, really was the component of the richfaces that was giving the problem.

  • @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.

Show 1 more comment

1 answer

1

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

To resolve the issue, simply remove the functionality of Ajax button, or changing the a4j:commandButton by a h:commandButton or any other component calling the action using the Submit form. With this the user will not change View and see the file download dialog.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.