miércoles, 24 de febrero de 2010

IE6 + HTTPS + download files = bug

The problem:
When you try to download a file using href tag in a HTTPS connection with Internet Explorer 6 you get this error: "Internet Explorer Cannot Download".

The Microsoft support link: http://support.microsoft.com/kb/812935/

My initial HTML code:

<a href="../files/test.zip">download</a>

The solution:
Make a specific "servlet" to perform the download. And change there the cache parameters in the request.

The official support says that error hapen with IE6 SP1 but I get the same situation with IE6 SP2.


<html>
<head>
<%@page import="java.util.*,java.io.*, javax.activation.MimetypesFileTypeMap"%>
<%@ page language="java" session="false" contentType="text/html; charset=8859_1"%>
</head>
<body>
<%
String fileName=request.getParameter("fileName");
File f=new java.io.File(request.getRealPath("/ficheros")+"/"+fileName);

if(!f.exists()){
%>
<script>alert("Error: Fichero ['<%=fileName%>'] inexistente!");</script>
<%
}else{
//the content type set as excel
response.setContentType (new MimetypesFileTypeMap().getContentType(f));
//the header and also the Nameis set by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename="+fileName);
response.setHeader("Content-Disposition", "inline; filename="+fileName);
response.setHeader("Expires", "0");
//response.setHeader("Cache-Control", "must-revalidate, post-check=0,pre-check=0");
response.setHeader("Pragma", "public");
// response.setDateHeader("Expires", 0); //prevents caching at the proxy
response.setHeader("Cache-Control", "max-age=0");

InputStream isStream = null;
ServletOutputStream sosStream = null;
try{
//response.flushBuffer();
isStream = new java.io.FileInputStream(f);
sosStream = response.getOutputStream();
int ibit = 256;
while ((ibit) >= 0){
ibit = isStream.read();
sosStream.write(ibit);
}
}catch (java.lang.Exception ioeException){%>
<script>alert('Se ha producido un error inesperado.!');</script>
<%}
sosStream.flush();
sosStream.close();
isStream.close();
}
%>
</body>
</html>



Now works with all browsers without errors.








No hay comentarios: