Can I check when a file is finished downloading with Java Script?

Hello Everyone!

I am fairly new to the world of software development, and especially JS. I’m building a paint app. When the user tries to the page, I have preventDefault() run beforeunload.
If the user clicks cancel, in the dialog, then a swal message pops up. It gives the user three options. “Save” “Don’t Save” and “Cancel” when the user clicks “Save” I have a download dialog open. When the download finishes, I want to display another swal that tells the user their file is saved. A lot of different forum posts I read from several years ago say it’s impossible to tell when a file is done download without server side code (I don’t know a thing about server side). But all these posts are so old, I can’t help wondering if something’s changed, possibly with the coming of ES6 or 7.

Does anybody have any non-server side solutions? Here’s my code:

Java Script

window.addEventListener('beforeunload', (event) => {
    const blank = document.createElement('canvas');
    blank.width = canvas.width;
    blank.height = canvas.height;

    if (canvas.toDataURL() !== blank.toDataURL()) {
        event.preventDefault();
        Swal.fire({
            title: "Do you want to save the changes?",
            showDenyButton: true,
            showCancelButton: true,
            confirmButtonText: "Save",
            denyButtonText: `Don't save`
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                const link = document.createElement("a");
                link.download = `Untitled${Date.now()}.jpg`;
                link.href = canvas.toDataURL();
                link.click().then((result) => {
                    Swal.fire("Saved!", "", "success");
                })
            } else if (result.isDenied) {
                Swal.fire("Changes are not saved", "", "info");
            }
        });
    }
});

Thanks For Helping!

Well, this isn’t really a download from a server, it’s just a blob that’s generated on the client side. As such, I would say the download should be finished nearly instantly, as all the data is already on the PC locally, it just needs to write it to disk.

Why do you ask? What are you trying to achieve?

When the user clicks “OK” in the download dialog, I want the dialog to disappear and the swal “Your image is saved!” popup to display.

Yeah I’m fairly certain you can show that and the file will already be on their machine or arrive within a few ms.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.