您的位置:首页 > Web前端 > JavaScript

js模拟用户多次点击下载文件

2016-04-21 17:00 531 查看
/**
 * Javascript 多文件下载
 * 动态创建a标签,模拟用户多次点击
 */
function download_files(files) {
    function download_next(i) {
        if(i>=files.length) {
            return;
        }
        var a = document.createElement('a');
        a.href = files[i];
        a.target = '_parent';
        // Use a.download if available, it prevents plugins from opening.
        if ('download' in a) {
            a.download = files[i].filename;
        }
        // Add a to the doc for click to work.
        (document.body || document.documentElement).appendChild(a);
        if (a.click) {
            a.click(); // The click method is supported by most browsers.
        } else {
            $(a).click(); // Backup using jquery
        }
        // Delete the temporary link.
        a.parentNode.removeChild(a);
        // Download the next file with a small timeout. The timeout is necessary
        // for IE, which will otherwise only download the first file.
        setTimeout(function () { download_next(i + 1); }, 500);
    }
    // Initiate the first download.
    download_next(0);
}

//调用时传入file文件名数组

function multiDown(){
var contentsId = $("#contentsId").val();
if(contentsId == "" || contentsId == null){
return ;
}
//E07007
if($("input[type='checkbox'][name='fileNameCk']:checked").length == 0){
warningShow("E07007",getErrorMessage("0","E07007",""));
return;
}
//E07006
confirmationShow("E07006",getErrorMessage("0","E07006",""),function(){
var fName = new Array();
$("input[type='checkbox'][name='fileNameCk']:checked").each(function(){
fName.push($(this).val());
})
//E00008
var files = new Array();
for(var i = 0;i<fName.length;i++){
files.push(
"ContentsDetail!download2.action?status=multi&" +
"fileAttachFileName="+encodeURIComponent(fName[i])+"&contentsDto.contentsId="+$("#contentsId").val()
);
}
download_files(files);
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js