const xhr = new XMLHttpRequest();
xhr.open('GET', props.downLoadUrl, true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (xhr.status === 200) {
const blob = xhr.response;
const urlCreator = window.URL || window.webkitURL;
const downloadLink = document.createElement('a');
downloadLink.href = urlCreator.createObjectURL(blob);
downloadLink.download = getFileNameFromUrl(props.downLoadUrl);
downloadLink.click();
btnColor.value = 'rgb(64, 158, 255)';
// 释放URL对象以避免内存泄漏
urlCreator.revokeObjectURL(downloadLink.href);
} else {
console.error('Error downloading file:', xhr.statusText);
}
};
xhr.onerror = function () {
console.error('Error downloading file:', xhr.statusText);
};
xhr.send();
如何取消下载文件的beforeunload事件
最新推荐文章于 2024-07-18 17:32:29 发布