2011-03-03 127 views
13

我使用plupload在客户端上传之前缩放图片。我喜欢这个功能,如果用户没有安装flash,silverlight等引擎,它会优雅地回退到html4。使用plupload手动触发“打开文件对话框”

我希望能够开始上传,当用户点击页面上的某些元素,我想处理事件(有时停止打开文件对话框)。事实上,我想弹出使用JavaScript打开文件对话框。除非用户点击浏览按钮(或覆盖了浏览按钮的覆盖层),否则HTML4(或者说浏览器,除了Chrome:P)不会让我这样做,所以当我得到回到HTML4我会接受,我不能这样做,但大多数用户将安装Flash或Silverlight,他们没有这个限制。所以我的问题是这样的:

我如何触发plupload中的文件打开对话框(记住我只需要flash和silverlight引擎来做到这一点)。

回答

4

如果有人搜索的HTML5解决方案,那就是:

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(); 
    }); 
} 
+2

是的,HTML5是现代浏览器的最佳选择(Flash,Silverlight和HTML4只是后备运行时)。由于我没有使用''pluploadQueue()'',所以我使用'plupload.Uploader''实例来获取输入的ID并触发点击甚至获得Select Files对话框(下面的答案)。 – Ilija 2013-10-14 14:36:56

1
+0

目前您的解决方案有关的JavaScript/HTML4和实际上都不为我所需要的情况下工作。我知道我的问题是非常具体的,但我只需要pluploads silverlight和flash引擎中的此功能,而不是html4 – 2011-03-15 22:47:46

2

确定。这似乎并不可能做到这一点,所以,除非有人实现对Silverlight和Flash组件我很倒霉的事件句柄

+2

实际上,Flash和Silverlight不会让您以编程方式触发文件对话框。它只能作为对鼠标点击等事件的反应发生(http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#browse())。 – jayarjo 2013-04-05 11:56:23

+1

@jayarjo - 文档状态方法抛出“错误 - 如果未响应用户操作调用方法,例如鼠标事件或按键事件。”它没有说明该鼠标事件或按键是否必须在闪光灯控制内。有谁知道是否可以通过按下外部按钮触发上传? – Mark 2013-12-11 03:26:37

5

随着时间的推移,回退运行时间将变得无关紧要。这意味着迟早我们将全部使用HTML5运行时。在您使用HTML5运行,但不使用pluploadQueue()情况下,这也能发挥作用:

// Set up and initialise uploader 
var uploader = new plupload.Uploader({ 
    'runtimes' : 'html5', 
    'browse_button' : 'id_of_the_first_button' 

    // Other options 
}); 

uploader.init(); 

// Hook in the second button 
plupload.addEvent(document.getElementById('id_of_the_second_button'), 'click', function(e) { 
    var input = document.getElementById(uploader.id + '_html5'); 
    if (input && !input.disabled) { 
    input.click(); 
    } // if 
    e.preventDefault(); 
}); 
+0

找到了一些对此和pluploadQueue的参考... http://www.plupload。com/punbb/viewtopic.php?pid = 6879#p6879 – CrandellWS 2014-08-05 13:28:22

+0

非常好。正是我所需要的 – robert 2014-08-14 03:08:54

14

前者的解决方案不是plupload 2.1.2曾在iPhone上。

下面的代码的伎俩(jQuery的需要):

$("#id_of_the_second_button").click(function() { 
    $('div.moxie-shim input[type=file]').trigger('click'); 
}); 
+0

谢谢你,这是我唯一的工作。 – Flezcano 2015-04-10 04:51:11

+0

也为我工作plupload()_not pluploadQueue()_ 这里是我的问题和链接到@estornes回答:) – Meloman 2016-04-29 15:43:05

+0

优秀,这应该是公认的答案! – 2017-06-10 02:21:21