Jquery中提交数据,并下载文件

/*===================下载文件
 * options:{
 * url:'',  //下载地址
 * data:{name:value}, //要发送的数据
 * method:'post'
 * }
 */
var DownLoadFile = function (options) {
    var config = $.extend(true, { method: 'post' }, options);
    var $iframe = $('<iframe id="down-file-iframe" />');
    var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
    $form.attr('action', config.url);
    for (var key in config.data) {
        $form.append(`<input type="hidden" name='${key}' value='${config.data[key]}' />`);
    }
    $iframe.append($form);
    $(document.body).append($iframe);
    $form[0].submit();
    $iframe.remove();
}

在页面中调用此方法,传入数据进行post提交,后端将文件data 和 文件name设置后直接返回即可

使用:

DownLoadFile({
    url: '/***/***',
    data: { id: id},
    method: 'post'
});