通过发送ajax请求下载Excel文件

我使用ajaxForm发送我的初始请求。 该方法被调用,所有的响应设置,但是当我试图打开窗口重新触发请求再次。 所以请求发送两次。以上是我的请求。

$('#reportForm').ajaxForm( { dataType :'json', type :'POST', url : 'report/initialRequest.html', beforeSubmit :validateSearchField, ifModified : true, success :function(data, textStatus, jqXHR){}, complete : function(jqXHR, textStatus) { window.location.href = "report/initialRequest.html" + "?" + $('#reportForm').formSerialize(); $.unblockUI(); return false; } }); 

有办法可以阻止第二个请求被发送。 这样做的全部目的是生成的报告太大,所以当用户提交请求时,jasper报告需要很长时间才能得到文件,所以用户不知道文件到达的时间。 所以我使用了一个块UI插件,当用户点击提交button时页面被阻塞,文件一回来就解除了页面的阻塞。

或者任何机构对如何实现这一目标有更好的想法。

控制器代码

@RequestMapping( “/报告/ initialRequest.html”)

 public @ResponseBody Map<String, Object> handleInitialRequest (HttpSession session, HttpServletRequest request, HttpServletResponse response ) { Collection<Results> results = getResults(); Map<String,Object> requestMap = new HashMap<String,Object>(); try { getReportDataAsExcel(session, request, response , results ); } catch (JRException e) { e.printStackTrace(); } requestMap.put("status", "SUCCESS"); return requestMap; } @SuppressWarnings("unchecked") 

public void getReportDataAsExcel(HttpSession session,HttpServletRequest request,HttpServletResponse response,Collection results)throws JRException {

  JRDataSource reportSource = new JRBeanCollectionDataSource( results ); Map parameters = new HashMap(); JRAbstractLRUVirtualizer virtualizer = null; // JRSwapFile swapFile = new JRSwapFile( getServletContext().getRealPath("/reports/"), 1024, 1024); JRSwapFile swapFile = new JRSwapFile( getServletContext().getRealPath("/reports/operationalreports/"), 1024, 1024); virtualizer = new JRSwapFileVirtualizer(2, swapFile, true); parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer); //logger.debug(reportUrl); Resource mainReport = null; JasperDesign design = null; JasperReport compiledReport = null; JasperPrint outputReport = null; try { mainReport = getApplicationContext().getResource(reportUrl); if (!mainReport.exists()){ throw new JRRuntimeException("File .jrxml was not found. The file must exists before compiling."); } InputStream reportInputStream = mainReport.getInputStream(); design = JRXmlLoader.load(reportInputStream); compiledReport = JasperCompileManager.compileReport(design); long start = System.currentTimeMillis(); logger.debug("Starting Time : " + start); outputReport = JasperFillManager.fillReport(compiledReport, parameters, reportSource); logger.debug("Filling time : " + (System.currentTimeMillis() - start)); writeExcel( session, request, response,outputReport ); if (virtualizer != null) { virtualizer.cleanup(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("unchecked") public void writeExcel(HttpSession session,HttpServletRequest request, HttpServletResponse response, JasperPrint jasperPrint) { ByteArrayOutputStream reportOutputStream = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE); JRExporter exporter = new JRXlsExporter(); // Excel specific parameters exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, reportOutputStream); try { exporter.exportReport(); } catch (JRException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } ServletOutputStream outputStream = null; InputStream is = null; byte[] res = reportOutputStream.toByteArray(); try{ response.setContentType(getResponseContentType()); setResponseHeader(response); response.setContentLength(reportOutputStream.size()); outputStream = response.getOutputStream(); is = new ByteArrayInputStream(res); int iSize = 0; byte[] oBuff = new byte[OUTPUT_BYTE_ARRAY_INITIAL_SIZE]; while ((iSize = is.read(oBuff)) != -1) { outputStream.write(oBuff, 0, iSize); } } catch ( Exception e){ e.printStackTrace(); } finally { try { outputStream.flush(); outputStream.close(); response.flushBuffer(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

我们有一个完整的页面请求生成一个Excel文件。 一旦请求完成,显示包含一个链接到生成的Excel文件。 我改变了这个过程,通过Ajax调用Excel生成页面,但是当这个过程完成时,它会把生成的Excel文件的URL返回给请求页面。 用户获得与链接的对话框,他们可以获得文件,而不需要两次运行Excel生成请求。 你可以改变你的过程像这样运行吗?

你不需要AJAX; 只需使用document.location="yourpage.php"

yourpage.php是您生成Excel文件的位置。