Tag Archives: Download files

How to download files by post submission under Ajax

/*===================Download file
 * options:{
 * url:'',  //Download Url
 * data:{name:value}, //the datas you want to post
 * 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();
}