.click() on <input type=“file” /> does not w

2019-05-23 10:02发布

问题:

Here is a little example: (live demo here)

HTML:

<input id="file" type="file" />

Javascript:

$(function() {
    $("#file").click();
});

In Firefox 3.6.6 nothing happens, while in IE7 the "Choose File" dialog box is opened.

Any ideas how to open the "Choose File" dialog box in Firefox without clicking on the Browse button ?

回答1:

The file-dialog breaks out of the sandbox your javascript code runs in (access to HDD). Good/secure browsers (= not IE7) should prevent that this dialog is opened via a script - it MUST be opened by a real mouseclick.

Some browsers seem to allow it, but it varies.

See more in In JavaScript can I make a “click” event fire programmatically for a file input element?



回答2:

this is fixed in Firefox Version 3.6.23