1. 使用IFrame
var _url = 'aa.com/download';
var fromstr = '<iframe id="d_iframe" name="d_iframe" style="display:none;"></iframe>';
$(fromstr).appendTo('body');
$('#d_iframe').attr('src', _url);

image.png
2. IFrame 模擬POST請求
(1). 方法
function postDownLoadFile (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();
}
(2) 調(diào)用
postDownLoadFile({
url: $('#exportUrl').val(),
data: {
Term: $("#Term").val(),
Course: $("#Course").val(),
Chapter: $("#Chapter").val(),
HWID: $("#HWID").val(),
PaperID: $("#PaperID").val(),
StudentName: $("#StuName").val(),
StudentCode: $("#StuCode").val(),
Status: $("#Status").val(),
BeginTime: $("#BeginTime").val(),
EndTime: $("#EndTime").val(),
ClassName: $("#ClassName").val(),
optionsStr: escape(dataTable.ajax.params().optionsStr),
extype: 'page'
},
method: 'post'
});
3. 使用a 標簽
var elink = document.createElement('a');
elink.download = '文件名';
elink.style.display = 'none';
elink.href = downUrl;
elink.target = "_blank";
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);

image.png
4.XMLHttpRequest下載

image.png
5.form表單提交下載
form表單是個比較常用的html表簽,用戶提交用戶信息,比如常見的登錄、注冊界面大部分都是通過form表單進行數(shù)據(jù)提交的。form表單所有要提交的數(shù)據(jù)都必須放在form標簽中,method屬性定義提交的方法(有g(shù)et和post兩種提交方法),action屬性定義請求的地址。form標簽中支持input、menus、textarea、fieldset、legend 和 label 等元素,通過submit向服務(wù)器提交數(shù)據(jù)。這里我創(chuàng)建了form表單和input框,input用于輸入請求的參數(shù),form用于提交數(shù)據(jù)請求

image.png