一、使用XMLHttpRequest,获取response 实现下载
(1)post
let that = this;
let params = {
taskId: that.fieId
};
let xhr = new XMLHttpRequest();
xhr.open('POST', '/ids/fap/session/pcap-export-download', true);
xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
xhr.responseType = 'blob';
xhr.onload = function (e) {
if (this.status == 200) {
let blob = this.response;
let a = document.createElement('a');
let url = window.URL.createObjectURL(blob);
a.href = url;
//获取后端文件名称
let fileName = decodeURI(xhr.getResponseHeader('content-disposition'));
console.log(fileName)//attachment;filename=日志文件20201211092152.zip
//截取=字符串后面的内容
let str = fileName.match(/=(\S*)/)[1];
console.log(str)
a.download = str;
a.click();
window.URL.revokeObjectURL(url);
}
};
//参数是json格式
xhr.send(JSON.stringify(params));
(2) get
let that = this;
let urlData = '/ids/report/exportWord' + "?startTime=" + that.report.startTime + "&endTime=" + that.report.endTime +"&type="+type
let xhr = new XMLHttpRequest();
xhr.open('GET', urlData, true);
xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
xhr.responseType = 'blob';
xhr.onload = function (e) {
if (this.status == 200) {
let blob = this.response;
let a = document.createElement('a');
let url = window.URL.createObjectURL(blob);
a.href = url;
let time = Util.timestampToTime(new Date(), 'YYYY-MM-DD HH_MM_SS')
a.download = name+time + '.doc';
a.click();
window.URL.revokeObjectURL(url);
}
};
xhr.send();
二、 构造form表单,form.submit() ,仅仅支持formData的参数格式
let form = document.createElement("form");
form.style.display = 'none';
form.action = '/hmonitor-web/warn/event/export';
form.method = "post";
document.body.appendChild(form);
//that.queryForm 参数
let objs = that.$.extend(true, {}, that.queryForm);
delete objs.currentPage;
delete objs.pageSize;
for (let key in objs) {
let input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = objs[key];
form.appendChild(input)
}
form.submit();
form.remove()
三、简单的get请求
window.location.href = ‘./download’ + “?remote=” +this.remote;