Skip to main content

Quick Tip: Content-Disposition

I ran into a problem that it took me a few hours to figure out, so maybe I can spare someone the headache. I'm creating a file using POI and streaming it to the browser. It was working great in Firefox, but in Internet explorer if I opened the file instead of saving it, the file name was xAgent_PrintForm.xsp and not the correct Form.docx. If I saved the file, it had the correct file name. Even worse, it had been working fine before and just started happening without any explanation. It turned out to be a header in my response writer.


    public void writeResponse(XWPFDocument doc, XspHttpServletResponse pageResponse, String fileName) {
        try {
            OutputStream pageOut = pageResponse.getOutputStream();
            pageResponse.reset();
            pageResponse.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            pageResponse.setHeader("Cache-Control", "no-cache");
            pageResponse.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            doc.write(pageOut);
            pageOut.flush();
            pageOut.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }


I had been setting my Content-Disposition header to "inline". That worked fine everywhere else, but in order to get IE to play nicely, I had to change it to "attachment".

Comments