Hello, I am trying to open the interface to select local files, the same one that opens when using the <input type="file"/>
but without using the input. I require that by means of a command the interface is opened directly to select files.
I've tried to directly create a File object but it asks me for two arguments, and I can't find much information about it.
let myFile = new File();
Uncaught TypeError: Failed to construct 'File': 2 arguments required, but only 0 present.
I also tried to simulate a click event on an input that is not added to the DOM but it doesn't work either:
var input = document.createElement('input');
input.setAttribute('type', 'file');
let click = new Event('click');
input.dispatchEvent(click);
I was hoping this would open the file selection window for me. but it doesn't work. any ideas?
Indeed it is not possible to start the file picker by an event that does not come from the user. even if the event comes from another element, trying it in chrome will return this error in the console:
in my case I needed to start the window through a keyboard shortcut so I took advantage of that event to simulate the click on a
<input type="file">
The following code is from a google extension:
background.js
contentScript.js
and this worked great for me!