1

I have a jsf app and when the user clicks a button, there is a backing bean method which calls this servlet.

The problem is that in IE7 and IE8, the file is unable to be downloaded. It gives the error message:

Internet Explorer cannot download ExcelGenerator from mydomain.com

What do i need to do to get this to work in IE7/IE8? Do i need to set some additional headers?

Backing bean method which calls servlet:

public void generateExcel()
{
    FacesContext context = FacesContext.getCurrentInstance();
    String url = context.getExternalContext().getRequestContextPath() + "/ExcelGenerator";
    context.getExternalContext().redirect(url);
}

Servlet that generates the excel file:

 public class ExcelGenerator extends HttpServlet {

        private static final long serialVersionUID = 1L;
        private static final Logger LOG = LoggerFactory.getLogger(ExcelGenerator.class);

        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
        }

        @SuppressWarnings("unchecked")
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            SXSSFWorkbook wb = new SXSSFWorkbook();
            Sheet sheet = wb.createSheet("Results");
            List<List<TableData>> results = (List<List<TableData>>) request.getSession().getAttribute("results");
            String tableName = (String) request.getSession().getAttribute("tableName");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String filename = tableName+"-"+df.format(date)+".xlsx";
            OutputStream out = null;

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Expires:", "0"); // eliminates browser caching
            response.setHeader("Content-Disposition", "attachment; filename="+filename);

            int rowCounter = 0;

            // Create the rows
            for(List<TableData> l : results) {

                int counter = 0;
                Row row = sheet.createRow(rowCounter);

                for(TableData td : l) {

                    Cell cell = row.createCell(counter);

                    // if we're on the first row, get the column description, else get the data
                    if(rowCounter == 0) {
                        cell.setCellValue(td.getColumnDescription());
                    } else {
                        cell.setCellValue(td.getData());
                    }
                    counter++;
                }
                rowCounter++;
            }

            try {
                out = response.getOutputStream();
                wb.write(out);
                out.flush();
            } catch (Exception e) {
                LOG.debug(e.getStackTrace().toString());
            } finally {
                out.close();
                wb.dispose();
            }
        }
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Catfish
  • 18,876
  • 54
  • 209
  • 353

3 Answers3

3

Internet Explorer cannot download ExcelGenerator from mydomain.

This error is typical when the domain is on HTTPS and the response doesn't allow caching. See also MS KB 316431.

The fix is rather simple:

if (request.isSecure()) {
    response.setHeader("Cache-Control", "public");
    response.setHeader("Pragma", "public");
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Take a look at this page which lists compatibility of recent browser versions with the Content-Disposition header. There are certain combinations of values, especially around how you encode the filename part, that can cause IE to display an error.

matt b
  • 138,234
  • 66
  • 282
  • 345
0

I have faced this issue and following entry resolve this problem

response.setContentType("application/vnd.ms-xls; charset=utf-8");

response.setHeader("Content-disposition","attachment;filename="+filename);

response.setHeader("Pragma", "public");

response.setHeader("Cache-Control", "public, max-age=3800");