Capturing the close of the browse for file window with JavaScript

前端 未结 3 1612
我在风中等你
我在风中等你 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:23

    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.

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

提交回复
热议问题