You can't determine download progress if you download the file that way.
If you download the file using XMLHttpRequest
, then you can listen for load, error and progress events like this:
function log(message) {
return function () {
alert(message);
};
}
function download(file, callback) {
var request = new XMLHttpRequest();
request.responseType = 'blob';
request.open('GET', file);
request.addEventListener('load', log('load ' + file));
request.addEventListener('error', log('error ' + file));
request.addEventListener('progress', log('progress ' + file));
request.addEventListener('load', function () {
callback(request.response);
});
request.send();
}
function save(object, mime, name) {
var a = document.createElement('a');
var url = URL.createObjectURL(object);
a.href = url;
a.download = name;
a.click();
}
document.querySelector('#download').addEventListener('click', function () {
download('test.pdf', function (file) {
save(file, 'application/pdf', 'test.pdf');
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<button id="download">Download</button>
<script src="script.js"></script>
</body>
</html>