473,326 Members | 2,013 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,326 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 31763

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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.