使用JavaScript捕获文件窗口的关闭

时间:2012-08-22 21:12:28

标签: javascript filedialog

我正在使用infile来要求用户在他们的机器上浏览文件。如果在没有选择文件的情况下关闭窗口,是否有办法捕获?
例如,如果单击x。

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

由于

3 个答案:

答案 0 :(得分:1)

使用HTML input file selection event not firing upon selecting the same file的解决方案,我认为你可以使用它:

<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)
}

答案 1 :(得分:0)

我做的是:  用户打开窗口后启动计时器,在该计时功能中,如果用boolean var选择了文件,我每0.5秒检查一次。一旦用户选择文件或发生HTML5 DnD,我就会停止计时器。我希望这有助于某人。

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

<button dojoType="dijit.form.Button" id="fileSelect" type="button" onmouseup="browse();">Browse</button> 

var fileselected = false;
function handleFiles(input){
    fileselected = true;
//use input
}

var interval;
function browse(){
    fileselected = false;
    var fileElem = document.getElementById("inFile");
    fileElem.click();
    interval = setInterval(setdiv, 500);

}
function setdiv(){
    if(!fileselected){  
        //do staff until user decides to Dnd or  browse for file again
        clearInterval(interval);
    }   
}

答案 2 :(得分:-2)

我认为blur事件会这样做:

<input
  type="file"
  onchange="changed=true; handleFiles(this)"
  onblur="if(!changed) alert('nothing selected'); changed=false;"
/>