0

使用 Firefox,当用户单击下载链接时,下载对话框会立即打开,但是在 Writer 上调用 close() 之前,我无法让 IE 显示“另存为”对话框。

我无法下载初始文件的大小,因为它是动态生成的。

ServletOutputStream os = null;
Writer writer = null;

try {
    os = response.getOutputStream(); // response is from an input param HttpServletResponse
    response.setHeader("Content-Disposition", "attachment");
    response.setContentType("text/csv; charset=windows-1252");
    writer = new OutputStreamWriter(os, Util.WINDOWS1252);
    writer.write("\u0000"); // flush() will be effective only if there is something in the buffer, this works with firefox, download dialog opens, but with not with IE
    writer.flush(); // forces to open the download popup instantly
    fooManager.processCSV(some params...,  writer); // generated on the fly and written in writer direclty
 } catch (IOException e) {
    logger.debug("csv: IOException ", e);
 } finally {
    if (writer != null) {
    try {
          writer.close(); // now IE will finally open download dialog...
    } catch (IOException e) {
          // ignore
    }
  }
}

谢谢!

4

1 回答 1

0

也许你需要文件名:

Content-Disposition: attachment; filename=FILENAME

并且,要强制打开另存为对话框,请添加:

Content-Type: application/force-download

或者:

Content-Type: application/octet-stream
于 2013-09-25T20:49:39.403 回答