方式一:使用iframe通过get方式下载
/**
* 功能:查看附件
*/
$("#check-enclosure-button").click(function(){
var checkedStrategy = $softDistributeTable.bootstrapTable('getSelections');
var len = checkedStrategy.length;
if(len == 0){
toastr.error("请选择软件", "错误提示");
}else if(len > 1){
toastr.error("一次只能查看一个附件", "错误提示");
}else if(len == 1){
var iframe = document.createElement("iframe");
iframe.style.display = "none"; // 防止影响页面
iframe.style.height = 0; // 防止影响页面
iframe.src = '/bmpf/softmng/download_attachment/?policy_id=' + checkedStrategy[0].id;
document.body.appendChild(iframe); // 这一行必须,iframe挂在到dom树上才会发请求
// 5分钟之后删除
setTimeout(function(){
iframe.remove();
}, 5 * 60 * 1000);
}
})
方式二:通过form通过post方式下载
function postExportFile(params, url) {
const form = document.createElement("form");
form.style.display = "none";
form.target = "_blank";
form.action = url;
form.method = "post";
document.body.appendChild(form);
for (var key in params) {
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = params[key];
form.appendChild(input);
}
form.submit();
form.remove();
}
const data = {
flag: 1,
// tokenid: localStorage.getItem("tokenid"),
username: this.loginName,
// doc_list: JSON.stringify(doclist),
// username: this.loginName,
depth: this.depth,
};
postExportFile(data, "/permtools/api/user/v1/export");