admin管理员组文章数量:1315061
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
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
Share Improve this question edited Aug 22, 2012 at 21:22 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Aug 22, 2012 at 21:12 setliosetlio 7262 gold badges14 silver badges32 bronze badges 1- Please clarify whether you mean the open-file-dialog or the whole browser window. – Bergi Commented Aug 22, 2012 at 21:30
3 Answers
Reset to default 2With 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)
}
What I did is: Start timer after the user opens the window, and in that timed function I check every 0.5 sec if the file has been selected with boolean var. I stop the timer as soon as user selects the file or HTML5 DnD occurs. I hope this helps someone.
<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);
}
}
I think the blur
event will do it:
<input
type="file"
onchange="changed=true; handleFiles(this)"
onblur="if(!changed) alert('nothing selected'); changed=false;"
/>
本文标签: filedialogCapturing the close of the browse for file window with JavaScriptStack Overflow
版权声明:本文标题:filedialog - Capturing the close of the browse for file window with JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971342a2407856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论