سؤال

As in this question, I got the page to work in Firefox, Chrome, IE9,...

However, IE8 is not working. I get an Error popup:

Internet Explorer cannot download [filename].jsp from [server].

Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Pleas try again later.

My code is as follows:

    public String downloadFile() { // called from a h:commandLink
    String filename = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("file");
    File file = new File(filename);
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

    writeOutContent(response, file);

    FacesContext.getCurrentInstance().responseComplete();
    return "REFRESH";
}

private void writeOutContent(final HttpServletResponse res, final File content) {
    if (content == null) {
        return;
    }
    try {
        res.setHeader("Pragma", "no-cache");
        res.setDateHeader("Expires", 0);
        res.setHeader("Content-Disposition", "attachment; filename=\"" + content.getName() + "\"");
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String contentType = servletContext.getMimeType(content.getName());
        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        res.setContentType(contentType);
        FileInputStream fis = new FileInputStream(content);
        ServletOutputStream os = res.getOutputStream();
        int length = 0;
        int data = fis.read();
        while (data != -1) {
            length += 1;
            os.write(data);
            data = fis.read();
        }
        fis.close();
        res.setContentLength(length);
        os.flush();
        os.close();
    } catch (final IOException ex) {
        Logger.getLogger(ApplicationController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The only thing I could think of is that I set the response content length after writing the file to the output stream, but as I said it works perfectly in modern browsers.

The thing is, we also have some javascript errors in IE8 that we can not get rid of. The rest of the page seems to work despite the errors, though. Errors are "SimpleTogglePanelManager undefined", and 2x "Object expected".

هل كانت مفيدة؟

المحلول

According to this Microsoft KB article it's by design.

The problem occurs if the server is using Secure Sockets Layer (SSL) and has added one or both of the following HTTP headers to the response message:

Pragma: no-cache

Cache-control: no-cache,max-age=0,must-revalidate

Removed res.setHeader("Pragma", "no-cache"); - works as intended.

See also this question and answer.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top