Programmatically invoke the browse dialog of input type=file

Discussion in 'Javascript' started by oopaevah@yahoo.co.uk, Sep 29, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    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!


    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, 2006
    #2
    1. Advertising

  3. Touffy Guest

    On 2006-09-30 15:31:46 +0200, 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
    Touffy, Sep 30, 2006
    #3
  4. 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, 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
    desprateCoder, Oct 13, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mathew George
    Replies:
    1
    Views:
    663
    Randy Charles Morin
    Sep 15, 2003
  2. =?Utf-8?B?ZGVuIDIwMDU=?=

    Getting File Path in Browse File Dialog in ASP.Net

    =?Utf-8?B?ZGVuIDIwMDU=?=, May 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    10,570
    =?Utf-8?B?ZGVuIDIwMDU=?=
    May 8, 2006
  3. Jen
    Replies:
    4
    Views:
    224
    Alessandro Zifiglio
    Feb 12, 2004
  4. Ron Brennan
    Replies:
    5
    Views:
    306
    Dr John Stockton
    May 14, 2004
  5. Replies:
    0
    Views:
    198
Loading...

Share This Page