Programmatically invoke the browse dialog of input type=file

O

oopaevah

I want to have a separate button which invokes the "browse" button on
an input type=file. In internet explorer the following code works ok,
in firefox nothing happens.

All I do is call click() for the input control.

Do you know how to make this work in firefox?

Thanks


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1" />
<script type="text/javascript">
function test()
{
var oTest = document.getElementById( "mytest");
oTest.click();
}
</script>
</head>

<body>

<form id="myform" action="test.aspx" method="post">
<input type="file" id="mytest" value="monkey" />
<br />
<input type="button" value="Programmatically Invoke The File
Browser Dialog" onclick="javascript:test();" />
</form>
</body>

</html>
 
O

oopaevah

Ok, it's not possible. In Gmail when you press the "attach a file"
link, in IE the browse dialog appears (programmatically invoked), in
Firefox the iframe housing the input file is displayed so the user can
click Browse themselves.

I didn't fancy doing a browser sniff to serve different behaviour so I
do the .click() and if the page hasn't lost focus within 50ms
(indicating that the file dialog has appeared) I show the iframe with
the "Browse" button on it.

Works great!
 
T

Touffy

Ok, it's not possible. In Gmail when you press the "attach a file"
link, in IE the browse dialog appears (programmatically invoked), in
Firefox the iframe housing the input file is displayed so the user can
click Browse themselves.

I didn't fancy doing a browser sniff to serve different behaviour so I
do the .click() and if the page hasn't lost focus within 50ms
(indicating that the file dialog has appeared) I show the iframe with
the "Browse" button on it.

Works great!

I had a doubt about Mac browsers behavior (since the file browser is
not strictly speaking a separate window) so I just tested and it seems
to work fine in Safari if only you give it more than 50ms. Safari
supports the click() method to display the file browser, but calls the
window.onblur handler only after the animation is complete.
In case you are not familiar with Mac OS X "attached" dialogs, they're
dialogs (usually open/save dialogs) that slide out of the title bar of
their parent window. This animation takes about one second to complete,
and the parent window remains focused during that time.

Firefox on Mac OS X doesn't use the standard attached dialog and anyway
it doesn't respond to click() on buttons so that would be irrelevant.

You should therefore give more time to the browser to blur the window
(1 second, maybe a bit more to be safe) to prevent Safari (and any
other Webkit-based browser) from displaying the form button even though
it did respond to your click()ing.

Have you tested if Firefox responds to click() on every other form
input elements ? Because if it doesn't, you could try click()ing
something with an onclick handler to test the functionnality instantly,
instead of waiting for a blur. Acutally, setting an onclick on the file
input element might do the trick.
 
D

desprateCoder

I saw the beta of Yahoo Mail was able to achieve this in Firefox, I
wonder how they do that? Because I need to do something like this also
in my project. I saw that they have a 'widgetbar' tag? I wonder what
that is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top