Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing the close of the browse for file window with JavaScript

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 is clicked.

<input type="file" name="data" id="inFile" size="15" style="display:none" onchange="handleFiles(this)"/>

Thanks

like image 490
setlio Avatar asked Aug 22 '12 21:08

setlio


Video Answer


1 Answers

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

<input type="file" name="data" id="inFile" />
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)
}
like image 103
Bergi Avatar answered Sep 30 '22 04:09

Bergi