SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    San Jose
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Triggering click() in input type="file" in Mozilla

    I would like to use a button form element to open the browse for file dialog box that opens when you click on the browse button of an input type="file" form element. I need this to be as cross-platform compatible as possible. This is perfectly simple in IE:

    Code:
    <form name="test">
    <input type="button" name="try_me" value="Choose File" onClick="document.test.uploadedFile.click();">
    <br>
    <br>
    <input type="file" name="uploadedFile" size="0" style="display: none;" onChange="document.test.filePath.value = this.value;">
    <br>
    <br>
    <input type="text" name="filePath" size="50" value="" style="border: 0px solid white;" onFocus="blur();">
    </form>
    But this code does not work in Mozilla, and there lies my problem. Apprently, the click() function is not supported by Mozilla for input elements with a type of "file". "click" is a property of the input element, but apparently doesn't do anything. Does anybody have a work-around for this?

    I've tried using a separate function, like this:

    Code:
     function openFileDialog()
         {
         fileInput = document.getElementById("uploadedFile");
         fileInput.click();
         }
    But I get the same result.

    My reasons for wanting this work-around are purely asthetical, I don't have to have it but it would be much easier to fit into the proposed design.

    Thanks in advance for any help you can offer. Please tell me if I've failed to make my question clear or haven't provided enough information for you to give me an answer.

    Best,

    Mike

  2. #2
    SitePoint Guru bronze trophy blufive's Avatar
    Join Date
    Mar 2002
    Location
    Manchester, UK
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a known issue with Mozilla-based browsers: http://bugzilla.mozilla.org/show_bug.cgi?id=36619

    It's also caught up in the slipstream around another general issue: The mozilla developers are (rightly, IMO) very twitchy indeed about the idea of any form of scripting in conjunction with file upload inputs - because if they make a mistake, it's a security hole the size of a planet.

    As a result, many superficially quite similar requests (e.g. javascript populating input type=file) will get marked as "WONTFIX" in no time flat.

    This particular bug report is still open after 4 years though, so there's a chance it will still get fixed/implemented.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •