因為使用ajax導出excel會出現問題,是以現在使用jQuery.fileDownload.js插件來解決導出excel的問題
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
在頁面引入jquery.fileDownload.js插件
1、如下所示
jquery-file-Download.js源碼解析:
2、在背景代碼中設定Cookie,并傳回Cookie的值
public
void export(final HttpServletRequest request, HttpServletResponse
response,final String semesterId) throws IOException,
IllegalArgumentException, IllegalAccessException {
String fileName = "excel檔案";
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "p_w_upload;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
//在這裡加入設定Cookie -------------
Cookie fileDownload=new Cookie("fileDownload", "true");
fileDownload.setPath("/");
response.addCookie(fileDownload);
//------------------------------------
ServletOutputStream out = response.getOutputStream();
final HSSFWorkbook workbook = new HSSFWorkbook();
List<Future<Boolean>> resultList = new ArrayList<Future<Boolean>>();
3、如果要使回調函數successCallback和failCallback起作用,還得在背景代碼中傳回Cookie
jquery-file-Download.js源碼解析:
背景設定與特定的cookie值
前台js定時去調用checkFileDownloadComplete方法,檢查前台與背景傳回的cookie值是否比對