Per*_*eck 14 javascript plupload
我在上传之前使用plupload来对客户端进行缩放.如果用户没有安装flash,silverlight等引擎,我喜欢它优雅地回退到html4的功能.
我希望能够在用户单击页面上的某些元素时启动上传,并且我想处理事件(有时会停止打开文件对话框).实际上我想使用javascript弹出文件对话框.
好的,所以除非用户点击浏览按钮(或覆盖浏览按钮的覆盖),否则HTML4(或者说浏览器,除了chrome:P)不允许我这样做,所以当我得到回退到HTML4时我会接受我不能这样做,但大多数用户将安装闪光灯或Silverlight,他们没有这个限制.所以我的问题是:
如何在plupload中触发文件打开对话框(请记住,我只需要flash和silverlight引擎来执行此操作).
小智 14
以前的解决方案不适用于带有plupload 2.1.2的iPhone.
以下代码完成了这个技巧(需要jquery):
$("#id_of_the_second_button").click(function() {
$('div.moxie-shim input[type=file]').trigger('click');
});
Run Code Online (Sandbox Code Playgroud)
随着时间的推移,后备运行时将变得无关紧要.这意味着我们迟早会全部使用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();
});
Run Code Online (Sandbox Code Playgroud)
如果有人正在搜索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();
});
}
Run Code Online (Sandbox Code Playgroud)