-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to know when a download is started?? #796
Comments
Hi! |
Hi, no, I mean an event. Because the download takes a few seconds to start, it would be great knowing when it's actually starting. |
What if we add performance.now() before and after script, like in below example. document.querySelector('#download-file')
Is this what you mean ? |
mmm no, I mean this:
I wanted an event that happens only when the actual download starts. So I can disable the button and reenable when it's emitted. I see your library uses xmlhttprequest, and there is an event for "progress" that could emit an event on the filesaver object for the first time or something like that. |
It shouldn't really be up to filesaver.js to download things. it should only save blobs if you need more adv download compatibility. then use xhr and download things yourself. const thingsToDownload = [
'http://httpbin.org/image/png',
'http://httpbin.org/image/jpeg',
// ...
].entries()
const blobs = []
async function download (iterator, i) {
for (let [index, item] of iterator) {
await new Promise(rs => {
const xhr = new XMLHttpRequest()
xhr.open('GET', item)
xhr.responseType = 'blob'
xhr.onload = () => {
blobs[index] = xhr.response
rs()
}
xhr.onprogress = evt => {
if (evt.lengthComputable) {
console.log(`Worker#${i}: ${index},${item} ${evt.loaded}/${evt.total}`)
} else {
console.log(`Worker#${i}: ${index},${item} ${evt.loaded}`)
}
}
})
}
}
// download 2 files at a time concurrently
const workers = Array(2).fill(thingsToDownload).map(download)
disable_download_button()
await Promise.allSettled(workers)
const zipFile = await create_zip_file(blobs)
enable_download_button()
saveAs(zipFile, 'things.zip') |
Nice guide, thanks |
My backend have to download data from several sources and build a zip file in realtime.
It takes a few seconds to prepare the download, so when the user clicks the button to save it, it's very easy for users to spam the button and trigger several downloads.
I know I can just disable the button for a few seconds, but it's a guessing solution.
If an event triggered when the download actually starts I could keep the button disabled for the correct amount of time.
The text was updated successfully, but these errors were encountered: