0

I'm working on porting some js code that works correctly in Chrome to Firefox. I have a following function for reading files from a file system:

function chooseFilesToOpen() {
    var fileInput = document.createElement('input');
    fileInput.type = 'file';
    fileInput.multiple = true;
    fileInput.addEventListener('change', function() {
        openFiles(fileInput.files);
    });

    fileInput.click();
}

The function is bound to a button on a page ($('#open-button').click(chooseFilesToOpen)) and to Ctrl+O shortcut (there's a switch statement for different keys). The function is invoked in both cases, upon Ctrl+O and a button click, but a file browsing dialog appears only in the latter. When I debug it, the dialog pops up when fileInput.click() is executed. The same statement seems to do nothing when the function is called from a keypress event handler. So, the question is how to make it work for both actions and in both browsers?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ivan
  • 588
  • 5
  • 20
  • How does it work when you do not even add it to the document? – epascarello Jan 21 '16 at 19:03
  • http://stackoverflow.com/questions/29589994/how-to-open-programmatically-the-file-dialog-box-in-firefox – epascarello Jan 21 '16 at 19:04
  • 1
    @epascarello, it's strange indeed that it works not being added to the document. I found another question close to mine: http://stackoverflow.com/questions/31032610/javascript-intercepted-ctrlo-does-not-open-my-file-dialog. It seems that it's not possible to simulate keyboard events that call file dialog in FF. I guess the question should be closed as a duplicate – Ivan Jan 22 '16 at 13:25

0 Answers0