I made a fiddle for your quick understood.
https://jsfiddle.net/bexoss/hdr5824a/7/
Is there a way to close file dialog using JavaScript?
I tried to send ESC key but the dialog is not dimissed.
<input id="input_file" type="file" />
<div></div>
<script>
// jQuery lib included in somewhere
$(document).on('click', '#input_file', function(e){
$('div').append('File dialog is opened. <br/>')
setTimeout(function(){
var esc = $.Event("keydown", { keyCode: 27 });
$('div').append('Escape key sent.<br/>')
$("body").trigger(esc);
}, 2000)
})
</script>
ESCwill not work here as you trigger ti in your thread but the file dialog is browser's part and is out of your sandbox.