9

We can use file.onchange if we gonna set an event callback for file reading using javascript, but how to set event for when user cancel the upload (close the browse panel)?

3
  • Perhaps if you mention your use case, somebody could suggest an alternative. Commented Jul 13, 2012 at 5:19
  • i just want to send the file without redirecting, so i have to use iframe. but i want the iframe to be built just after the user click <input/> and disappear right after user finish using the panel. that's the idea Commented Jul 13, 2012 at 8:04
  • Hmm, so are you wanting to trigger the upload as soon as the user selects something? My suggestion would be to let the user pick the file, then right next to it - have a upload button. Then you can create the iframe when they click that. That way you know the value of the file selector, and obviously if it is nil, you can present some sort of validation message. Commented Jul 13, 2012 at 18:07

3 Answers 3

1

That’s a great solution:

    const createUpload = () => {
        let lock = false
        return new Promise((resolve, reject) => {
            const el = document.createElement('input');
            el.id = +new Date();
            el.style.display = 'none';
            el.setAttribute('type', 'file');
            document.body.appendChild(el)

            el.addEventListener('change', () => {
                lock = true;
                const file = el.files[0];
                resolve(file)
                document.body.removeChild(document.getElementById(el.id));
            }, { once: true })

            window.addEventListener('focus', () => { // file blur
                setTimeout(() => {
                    if (!lock && document.getElementById(el.id)) {
                        reject(new Error('onblur'))
                        document.body.removeChild(document.getElementById(el.id))
                    }
                }, 300)
            }, { once: true })
            el.click() // open file select box
        })
    }

Ussage:

    // $label.classList.add('loading');
    createUpload()
        .then(function (file) {
            // Sent file 
            // $label.classList.remove('loading');           
        })
        .catch(function (err) {
            // Your Cancel Event
            // $label.classList.remove('loading');
        });
Sign up to request clarification or add additional context in comments.

Comments

0

There is no API for the file input modal. Besides, if the user closes the browser your code won't be running anymore, will it?

Of course there is the window.onunload method which allows you to detect the example you give.

Per the comments, the best thing I can come up with that would be helpful is that if nothing is selected, file.value.length will be 0.

6 Comments

im sorry. it's not browser. what i mean is file browse panel. which could be opened when we click on <input type='file'/> element
No worries. Unfortunately, there is no cancel event. The only thing I've come up with that would be helpful is that if nothing is selected, file.value.length will be 0. Unfortunately, you have nothing to detect when the user exits the modal except for onchange, which as you point out only fires when a file is selected.
unfortunately the value doesnt change between before and after the file panel popup. so even with setinterval we cannot catch the popup. do you think there is any other values would be changed right after the panel popup?
I tried a few, all of the mouse props, onmousedown, onmouseup, over, out, etc. All of them are done before the popup is even generated.
"file.value.length will be 0" by @JonJaques is the answer I was looking for. Should be injected in the response IMHO.
|
-3

It is very simple with jQuery :

$("#fileInputId").change(function () {
    //implement your code here
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.