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(文件路径);
07-16
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交