jquery.fileDownload.js外掛程式匯出excel

來源:互聯網
上載者:User

標籤:excel   download   jquery-file-download   

因為使用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的值

650) this.width=650;" src="http://blog.csdn.net/wyc111/article/details/47613465" />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", "attachment;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 mssetTimeout(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;    }

本文出自 “ciyo技術分享” 部落格,請務必保留此出處http://ciyorecord.blog.51cto.com/6010867/1934849

jquery.fileDownload.js外掛程式匯出excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.