ajax批量下載檔案,動態構建一個Form 並且提交獲取返回的檔案流
阿新 • • 發佈:2019-01-06
//批量下載檔案(打包)
function button_downloadplus(url) {
var rows = $('#exampleTableEvents').bootstrapTable('getSelections'); //返回所有選擇的行,當沒有選擇的記錄時,返回一個空陣列
var $table = $("#exampleTableEvents");
//alert("進入reload");
//return;
if (rows.length == 0) {
alert("請選擇要下載的資料", "error");
return;
};
var title = "您確定要下載選定的" + rows.length + "個檔案及資料夾嗎"
swal({
title: title,
text: "此下載佔用系統資源,請勿頻繁操作!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "是的,我要下載!",
cancelButtonText: "讓我再考慮一下…",
closeOnConfirm: false,
closeOnCancel: false
}, function (isConfirm) {
if (isConfirm) {
post_url = url + "&time=" + new Date().getTime();
var ids = '';
//遍歷所有選擇的行資料,取每條資料對應的ID
$.each(rows, function(i, row) {
var file_type = 2;
if(row['file_type']=='資料夾'){
file_type = 1;
}
ids = ids + '{"id":' + row['id'] + ',"file_type":' + file_type +'};';
});
console.log(ids);
if(ids!=''){
ids = ids.slice(0,ids.length-1);}
//定義ajax請求引數
var param = {ids:ids};
console.log(param);
swal.close();
$("#loadingModal").modal('show');
$.dynamicSubmit(post_url,param);
$("#loadingModal").modal('hide'); //**********************分2步走,先伺服器POST提交伺服器打包檔案,打包完成返回引數給客戶端在下載打包後的值
} else {
swal("已取消", "您取消了刪除操作!", "error")
}
})
}
/*
* 動態構建一個Form 並且提交
*/
$.dynamicSubmit = function (url, datas) {
var form = $('#dynamicForm');
if (form.length <= 0) {
form = $("<form>");
form.attr('id', 'dynamicForm');
form.attr('style', 'display:none');
form.attr('target', '');
form.attr('method', 'post');
$('body').append(form);
}
form = $('#dynamicForm');
form.attr('action', url);
form.empty();
if (datas && typeof (datas) == 'object') {
for (var item in datas) {
var $_input = $('<input>');
$_input.attr('type', 'hidden');
$_input.attr('name', item);
$_input.val(datas[item]);
$_input.appendTo(form);
}
}
form.submit();
}
function button_downloadplus(url) {
var rows = $('#exampleTableEvents').bootstrapTable('getSelections'); //返回所有選擇的行,當沒有選擇的記錄時,返回一個空陣列
var $table = $("#exampleTableEvents");
//alert("進入reload");
//return;
if (rows.length == 0) {
alert("請選擇要下載的資料", "error");
return;
};
var title = "您確定要下載選定的" + rows.length + "個檔案及資料夾嗎"
swal({
title: title,
text: "此下載佔用系統資源,請勿頻繁操作!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "是的,我要下載!",
cancelButtonText: "讓我再考慮一下…",
closeOnConfirm: false,
closeOnCancel: false
}, function (isConfirm) {
if (isConfirm) {
post_url = url + "&time=" + new Date().getTime();
var ids = '';
//遍歷所有選擇的行資料,取每條資料對應的ID
$.each(rows, function(i, row) {
var file_type = 2;
if(row['file_type']=='資料夾'){
file_type = 1;
}
ids = ids + '{"id":' + row['id'] + ',"file_type":' + file_type +'};';
});
console.log(ids);
if(ids!=''){
ids = ids.slice(0,ids.length-1);}
//定義ajax請求引數
var param = {ids:ids};
console.log(param);
swal.close();
$("#loadingModal").modal('show');
$.dynamicSubmit(post_url,param);
$("#loadingModal").modal('hide'); //**********************分2步走,先伺服器POST提交伺服器打包檔案,打包完成返回引數給客戶端在下載打包後的值
} else {
swal("已取消", "您取消了刪除操作!", "error")
}
})
}
/*
* 動態構建一個Form 並且提交
*/
$.dynamicSubmit = function (url, datas) {
var form = $('#dynamicForm');
if (form.length <= 0) {
form = $("<form>");
form.attr('id', 'dynamicForm');
form.attr('style', 'display:none');
form.attr('target', '');
form.attr('method', 'post');
$('body').append(form);
}
form = $('#dynamicForm');
form.attr('action', url);
form.empty();
if (datas && typeof (datas) == 'object') {
for (var item in datas) {
var $_input = $('<input>');
$_input.attr('type', 'hidden');
$_input.attr('name', item);
$_input.val(datas[item]);
$_input.appendTo(form);
}
}
form.submit();
}