473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 31781

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
4
by: Paul Moffitt | last post by:
is it possible to define a default or start up directory with input type="file" field type? If so, is it also possible to filter by another option box selection? If so, how so? Thank you ...
2
by: Evgeny Zoldin | last post by:
Hi ALL. I need to simulate send POST data ( - file upload) as it was submitted in Browser. In C# I create instanse of IE, navigate to page with the form, fill in the fields and try to Submit by...
2
by: Ersin Gençtürk | last post by:
hi , I need to customize <input type=file , tag. I find a way to do that : on the client side by javascript and css first , I am hiding the fileupload object , then I am calling the click...
5
by: hrpreet | last post by:
Hi All, I need the file chooser in the jsp, just for brosing and saving the file path in the database, so i have used the following code.I dont need to read the file content. I have to make it...
9
by: Prakash Singh Bhakuni | last post by:
am replacing the default "Browse..." button for input type=file. This works fine except that the form will only submit after the SUBMIT button is clicked twice. Any ideas on why this is happening...
6
by: reiyel | last post by:
hiya all i would like to ask some help for a little javascript, here's my problem: i have a html page with a <input type="file"> box, problem is i don't want the user to be able to write on the...
9
by: Roger Withnell | last post by:
I am building a website in Russian. What options do I have to make the "Browse" button display in other languages? Thanking you in anticipation. Posted Via Usenet.com Premium Usenet...
5
by: magix | last post by:
Hi, with: <Input type="file"...> it will let the user to click to "browse" button to browse for files, question is how can I limit the file type to be only jpg and gif extension, when the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.