js发送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();
}

//调用 DownLoadFile({ url:
'xxx', //请求的url data:{a:'xx'}//要发送的数据 });

猜你喜欢

转载自www.cnblogs.com/Doduo/p/9483254.html
今日推荐