利用jQuery 通用文件導出前端實現,MVC文件導出


 

 

/**
 * 下載文件
 * @param options
 * =============================
 *  downLoadFile({
 *      url:'', //下載地址
 *      data:{"id":"4f4fa1d55b114d8cb9a5e46d13b5e5bc"},//請求參數
 *      isNewWinOpen:false,//是否新窗口打開
 *      method:'post'
 *  });
 *
 *
 * =============================
 */
function downLoadFile(options) {
    var config = $.extend(true, {
        method: 'post',
        isNewWinOpen: false,
        onLoad:function () {

        }
    }, options);
    var frameName = 'downloadFrame_' + new Date().getTime();
    var $iframe = $('<div style="display: none"><iframe name="' + frameName + '" src="about:blank"></iframe></div>');
    var $form = $('<form target="' + frameName + '" method="' + config.method + '" action="' + config.url + '"></form>');
    if (config.isNewWinOpen) {
        $form.attr("target", "_blank");
    }
    $iframe.children().load(function () {
        try {
            var jsonStr = $(this).contents('body').text();
            var jsonObj;
            if(jsonStr!=''){
                jsonObj= JSON.parse(jsonStr);
                config.onLoad(jsonObj);
            }
        } catch (e) {

        }
        setTimeout(function () {
            $(this).parent().remove();
        }.bind(this), 3000);
    });
    /*拼接參數*/
    for (var key in config.data) {
        $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
    }
    $iframe.append($form);
    $('body').append($iframe);
    $form.submit();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM