使用 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
}
}
}
谢谢!