javascript 使用 plupload 手动触发“打开文件对话框”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5182019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually trigger 'open file dialog' using plupload
提问by Per Hornsh?j-Schierbeck
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.
在上传图片之前,我正在使用 plupload 对图片进行客户端缩放。如果用户没有安装 flash、silverlight 等引擎,我喜欢它优雅地回退到 html4 的功能。
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.
我希望能够在用户单击页面上的某些元素时开始上传,并且我想处理这些事件(有时会阻止打开文件对话框)。事实上,我想使用 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:
好的,所以 HTML4(或者更确切地说是浏览器,除了 chrome :P)不会让我这样做,除非用户点击浏览按钮(或覆盖浏览按钮的覆盖层),所以当我回到 HTML4 时我承认我做不到,但大多数用户会安装 flash 或 silverlight,他们没有这个限制。所以我的问题是:
How do i trigger the file open dialog in plupload (keeping in mind i only need the flash and silverlight engines to do this).
我如何在 plupload 中触发文件打开对话框(请记住,我只需要 flash 和 silverlight 引擎来执行此操作)。
采纳答案by Léon Pelletier
If someone is searching for the HTML5 solution, here it is:
如果有人正在搜索 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();
});
}
回答by estornes
The former solutions not worked on iPhones with plupload 2.1.2.
以前的解决方案不适用于带有 plupload 2.1.2 的 iPhone。
The following code did the trick (jquery needed):
下面的代码做到了这一点(需要 jquery):
$("#id_of_the_second_button").click(function() {
$('div.moxie-shim input[type=file]').trigger('click');
});
回答by Ilija
Fallback runtimes will become irrelevant as times goes by. This means that sooner or later, we'll be all using HTML5 runtime. In case that you are using HTML5 runtime, but don't use pluploadQueue(), this will work as well:
随着时间的推移,回退运行时将变得无关紧要。这意味着迟早,我们都会使用 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();
});
回答by Per Hornsh?j-Schierbeck
Ok. It doesn't seem possible to do this, so unless someone implements event handles for the silverlight and flash components i'm out of luck
行。似乎不可能做到这一点,所以除非有人为 silverlight 和 flash 组件实现事件句柄,否则我很不走运
回答by Dilhan Maduranga
I read your problem.
我读了你的问题。
I found some articles that may help to figure this out. check them. It may help...!
我找到了一些可能有助于解决这个问题的文章。检查它们。它可能会有所帮助...!
02. https://stackoverflow.com/questions/2048026/open-file-dialog-box-in-javascript
02. https://stackoverflow.com/questions/2048026/open-file-dialog-box-in-javascript
回答by Vi?t Anh
@Per Hornsh?j-Schierbeck After uploader has been init. It take few time to render input file to select. So you need to wait like below:
@Per Hornsh?j-Schierbeck 上传器初始化后。渲染输入文件以供选择需要很少的时间。所以你需要像下面这样等待:
this.uploader.init();
var task = new Ext.util.DelayedTask(function () {
var inputArray = $('div.moxie-shim input[type=file]');
var input = inputArray.length > 1 ? inputArray[inputArray.length - 1] :
inputArray[0];
$(input).trigger('click');
});
task.delay(100);
The code in javascript is similar. Worked for me with plupload 2.3.6
javascript 中的代码是类似的。使用 plupload 2.3.6 对我来说有效
Hop this help!
跳这个帮助!

