472,121 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Programmatically invoke the browse dialog of input type=file

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>

Sep 29 '06 #1
3 31468

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!
oo******@yahoo.co.uk wrote:
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>
Sep 30 '06 #2
On 2006-09-30 15:31:46 +0200, oo******@yahoo.co.uk said:
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.

--
David Junger

Sep 30 '06 #3
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.

Touffy wrote:
On 2006-09-30 15:31:46 +0200, oo******@yahoo.co.uk said:
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.

--
David Junger
Oct 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Paul Moffitt | last post: by
2 posts views Thread by Ersin Gençtürk | last post: by
5 posts views Thread by hrpreet | last post: by
6 posts views Thread by reiyel | last post: by
9 posts views Thread by Roger Withnell | last post: by
5 posts views Thread by magix | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.