jquery.fileDownload.js插件导出excel
因为使用ajax导出excel会出现问题,所以现在使用jQuery.fileDownload.js插件来解决导出excel的问题
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
在页面引入jquery.fileDownload.js插件
1、如下所示
<script type="text/JavaScript" src="${resource}/js/jquery.fileDownload.js"/></script> <script type="text/javascript"> $("#export_confirm").on("click",function(){ var url="${path}/admin/information/student/export"; $.fileDownload(url,{ data:{semesterId:$("#misSemester").val()}, prepareCallback:function(url){ alert("正在导出,请稍后..."); }, successCallback: function(url){ alert("导出完成!"); }, failCallback: function (html, url) { alert("导出失败,未知的异常。"); } }); });
jquery-file-Download.js源码解析:
onPrepare: function (url) { //preparingMessageHtml属性存在,弹出导出准备提示框 if (settings.preparingMessageHtml) { //jueryUi dialog 可自己修改成其它的。 $preparingDialog = $("<div>").html(settings.preparingMessageHtml).dialog(settings.dialogOptions); } else if (settings.prepareCallback) { //调用回调函数 settings.prepareCallback(url); } }, //导出失败调用的函数 onFail: function (responseHtml, url, error) { //准备提示对话框存在则关闭 if ($preparingDialog) { $preparingDialog.dialog('close'); } //failMessageHtml存在弹出对话框提示 if (settings.failMessageHtml) { $("<div>").html(settings.failMessageHtml).dialog(settings.dialogOptions); } //调用回调函数 settings.failCallback(responseHtml, url, error); deferred.reject(responseHtml, url); }
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值是否匹配
//check if the file download has completed every checkInterval ms setTimeout(checkFileDownloadComplete, settings.checkInterval); function checkFileDownloadComplete() { //has the cookie been written due to a file download occuring? var cookieValue = settings.cookieValue; if(typeof cookieValue == 'string') { cookieValue = cookieValue.toLowerCase(); } var lowerCaseCookie = settings.cookieName.toLowerCase() + "=" + cookieValue; if (document.cookie.toLowerCase().indexOf(lowerCaseCookie) > -1) { //execute specified callback internalCallbacks.onSuccess(fileUrl); //remove cookie var cookieData = settings.cookieName + "=; path=" + settings.cookiePath + "; expires=" + new Date(0).toUTCString() + ";"; if (settings.cookieDomain) cookieData += " domain=" + settings.cookieDomain + ";"; document.cookie = cookieData; //remove iframe cleanUp(false); return; }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
日程日历系统,顶级插件FullCalendar使用说明
简介 日程系统,繁琐的月日期开始结束计算、当前页面开始的天数(跨月)。。。等等,麻烦的事情都交给FullCalendar,世界安静了。 官网 https://fullcalendar.io/ 使用 1、页面引用 <linkhref='/contrast/assets/global/plugins/fullcalendar-3.2.0/fullcalendar.min.css'rel='stylesheet'/> <linkhref='/contrast/assets/global/plugins/fullcalendar-3.2.0/fullcalendar.print.min.css'rel='stylesheet'media='print'/> <scriptsrc='/contrast/assets/global/plugins/fullcalendar-3.2.0/lib/moment.min.js'></script> <scriptsrc='/contrast/assets/global/plugins/fullcale...
- 下一篇
使用python实现统计Nginx进程所占用的物理内存
实现代码一: 此方法适用于对进行名统一的进行进行统计,如httpd,ngins或php-fpm等 #!/usr/bin/python #coding:utf8 fromsubprocessimportPopen,PIPE importos #如果需要对httpd进行统计可以把nginx改为httpd,其它服务统计同理,但有部分无法实现,如oracle nginxpid=Popen(["pidof","nginx"],stdout=PIPE) nginxpid=nginxpid.stdout.read().split() memsum=0 foriinnginxpid: pidfile=os.path.join("/proc/",str(i),"status") withopen(pidfile)asf: formeminf: ifmem.startswith("VmRSS"): pidmem=int(mem.split()[1]) memsum+=pidmem print("%d%s"%(memsum,"KB")) 实现代码二: 此方法适用于某个用户进行使用内存统计,最后的结果是,一个...
相关文章
文章评论
共有0条评论来说两句吧...