Search code examples
javascriptplupload

Manually trigger 'open file dialog' using plupload


I'm using plupload to client scaling of pictures before they are uploaded. I like the feature that it gracefully falls back to html4 if the user doesn't have flash, silverlight etc engines installed.

I want to be able to start the upload when the user clicks certain elements on the page and i want to handle the events (sometimes stopping a file dialog from opening). In fact i'd like to pop open the file dialog using javascript.

Ok, so HTML4 (or rather the browser, except chrome :P) won't let me do this, unless the user clicks a browse-button (or an overlay covering a browse-button), so when i get the fallback to HTML4 i'll accept that i can't do it, but most users will have flash or silverlight installed and they do not have this restriction. So my question is this:

How do i trigger the file open dialog in plupload (keeping in mind i only need the flash and silverlight engines to do this).


Solution

  • If someone is searching for the HTML5 solution, here it is:

    var up= $('#uploader').pluploadQueue();
    if (up.features.triggerDialog) {
        plupload.addEvent(document.getElementById('idOtherButton'), 'click', function(e) {
            var input = document.getElementById(up.id + '_html5');
            if (input && !input.disabled) { // for some reason FF (up to 8.0.1 so far) lets to click disabled input[type=file]
                input.click();
            }
            e.preventDefault();
        }); 
    }