473,786 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programmaticall y 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.dt d">
<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.getEle mentById( "mytest");
oTest.click();
}
</script>
</head>

<body>

<form id="myform" action="test.as px" method="post">
<input type="file" id="mytest" value="monkey" />
<br />
<input type="button" value="Programm atically Invoke The File
Browser Dialog" onclick="javasc ript:test();" />
</form>
</body>

</html>

Sep 29 '06 #1
3 31913

Ok, it's not possible. In Gmail when you press the "attach a file"
link, in IE the browse dialog appears (programmatical ly 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.dt d">
<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.getEle mentById( "mytest");
oTest.click();
}
</script>
</head>

<body>

<form id="myform" action="test.as px" method="post">
<input type="file" id="mytest" value="monkey" />
<br />
<input type="button" value="Programm atically Invoke The File
Browser Dialog" onclick="javasc ript: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 (programmatical ly 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 (programmatical ly 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
9929
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 replacing it with a button of my own background color and text. The file paths I'd like displayed in a textarea and then the files uploaded at once.
4
4589
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 Paul Moffitt
2
28395
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 call submitButton.click() Unfortunately it does not work while file_filed.vale = <path_to_file> has no effect, value <INPUT type=file...> cound not be set from script of that HTML page. Is there another way to set that value by means of ..NET ? Below...
2
2179
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 event of fileupload object.This opens the select files dialog box of file upload and user will be able to select the file that he wants to upload.But it seems that internet explorer does not allow uploading files selected by using this method.Because...
5
8036
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 hidden because in edit mode i need to show the field prepopulated with the file path, which doesnot happen with input type=file, it cant be shown prepopulated. But the following code gives access denied error.Please help.
9
24751
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 or a workaround? I'm testing on IE Thanks! <html> <head></head> <body> <form name="test_form" method="post" action="test.jsp">
6
8108
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 text field, he must use the browse button. i tried a lot of codes but no one works for both IE and firefox i tried to use onclick="blur",m but in that case the field and "browse button" didn't work here's what i've done: <form name="form1"> ...
9
17026
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 Newsgroup Services
5
14418
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 browse window open ? thank you.
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.