Capturing the close of the browse for file window with JavaScript

前端 未结 3 1769
我在风中等你
我在风中等你 2021-01-12 02:38

I am using infile to ask the users to browse for a file on their machine. Is there way to catch if the window is closed without file being selected?
For example if x i

3条回答
  •  情歌与酒
    2021-01-12 03:42

    With the solution from HTML input file selection event not firing upon selecting the same file, I think you can use this:

    
    
    var fileElem = document.getElementById("inFile");
    var fileSelected = null;
    fileElem.onclick = function(e) {
        fileSelected = this.value;
        this.value = null;
    });
    /* or use this in your browse() function:
        fileElem.value = null;
    */
    fileElem.onchange = function(e) { // will trigger each time
        handleFileDialog(this.value === fileSelected);
    };
    
    function handleFileDialog(changed) {
        // boolean parameter if the value has changed (new select) or not (canceled)
    }
    

提交回复
热议问题