load = function () {
//填写你的下载时加载的提示
}
disload = function () {
//下载完成后触发,用来关闭提示框
}
getDownload = function (url,fileName ) {
load();
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true); // 也可用POST方式
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status === 200) {
var blob = this.response;
console.log(blob);
if (navigator.msSaveBlob == null) {
var a = document.createElement('a');
//(fileName)设置下载时的文件名
console.log(fileName);
a.download = (fileName);
a.href = URL.createObjectURL(blob);
$("body").append(a); // 修复firefox中无法触发click
a.click();
URL.revokeObjectURL(a.href);
$(a).remove();
} else {
navigator.msSaveBlob(blob, (fileName););
}
}
disload();
};
xhr.send()
};
//调用:文件路径一般为项目可访问路径
getDownload(文件路径);
【JS】下载响应代码
最新推荐文章于 2023-01-05 16:48:56 发布