简体   繁体   中英

IE8 asks to Open/Save twice on .xls file

I'm generating an Excel document via Servlet. When I send the response back to the client (IE8), the "Open/Save" dialog pops up but requires users to click a choice twice before taking action. This doesn't happen in Firefox. I have no idea why this is occurring. Below is the relevant code that creates the appropriate streams.

result contains the Excel XML.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=TestFile.xls");

InputStream in = new ByteArrayInputStream(result.toString().getBytes("UTF-8"));
ServletOutputStream out = response.getOutputStream();

try
{
    byte[] outputByte = new byte[4096];

    while(in.read(outputByte, 0, 4096) != -1)
        out.write(outputByte, 0, 4096);
}
finally
{
    in.close();
    out.flush();
    out.close();
}

EDIT I have noticed that waiting 5+ seconds before clicking an option works just fine. It seems to only ask twice when immediately clicking an option.

This code works well for every type of file in my application

  InputStream in = blob.getBinaryStream();
  // Output the blob to the HttpServletResponse

  String codedfilename = "";
  //this code resolves the issue with the encoding of the downloaded filename
  String agent = request.getHeader("USER-AGENT");
  if (null != agent && -1 != agent.indexOf("MSIE"))
  {
    codedfilename = URLEncoder.encode(/*here goes the filename*/, "UTF8");
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition","attachment;filename=" + codedfilename);
  }
  else if (null != agent && -1 != agent.indexOf("Mozilla"))
  {
    response.setCharacterEncoding("UTF-8");
    //It does not seem to make a difference whether Q or B is chosen
    codedfilename = MimeUtility.encodeText(rset.getString("FILE_NAME"), "UTF8", "B");
    response.setContentType("application/force-download");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + codedfilename + "\"");
  }

  BufferedOutputStream out =
      new BufferedOutputStream(response.getOutputStream());
  byte by[] = new byte[32768];
  int index = in.read(by, 0, 32768);
  while (index != -1) {
      out.write(by, 0, index);
      index = in.read(by, 0, 32768);
  }
  out.flush();

try it and let us know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM