使用GWT一次上传多个文件 [英] Upload multiple files at once using GWT
问题描述
我拼命试图为我的网站的用户启用多个媒体资源。用户应该能够将一堆PDF文件上传到我的服务器,而无需单独选择每个文件。我正在使用 gwt-upload 进行上传。可悲的是,
MultiUploader
实际上并不允许用户一次选择多个文件,但您必须再次点击每个文件的浏览按钮并在浏览器中选择。
我研究并认识到我需要这样的东西
< input name ='uploads [] 'type =filemultiple>
在 gwt-upload 到目前为止已经完成了这个技巧,即打开的文件对话框允许我选择多个文件,但上传不会启动/工作
我找不到任何使用带gwt的多文件输入的示例。如果有办法通过 gwt-upload 执行此操作,这将是完美的,因为我没有真的很想自己实现整个上传逻辑!
非常感谢!
解决方案非常简单。 gwt-upload有一个类上传器,可以进行多次上传。在gwt-upload的wiki中建议的servlet代码已经能够处理多个上传。
我不得不改变Uploader类中的某些东西(源代码完全可用,所以我只是复制它并替换了我需要的部分)。为了访问所选文件的文件名和文件大小,我创建了一个简单的本地方法:
pre $ private $ static getFilesSelected()/ * {
var count = $ wnd。$('input:file')[0] .files.length;
var out =;
(i = 0; i< count; i ++){
var file = $ wnd。$('input:file')[0] .files [i];
out + = file.name +';'+ file.size +;;
}
退出;
} - * /;
哪个返回值I split by;以获得所需的结果。
您需要用自定义的替换上传器FileInput(setFileInput()),该属性将多个属性设置为输入。我使用这样的包装类:
public class MyFileInput实现了IFileInput {
private final FileUpload fu ;
public MyFileInput(){
fu = new FileUpload();
DOM.setElementProperty(fu.getElement(),multiple,multiple);
$ b你必须实现IFileInput的其他方法,我把他们全部连接到fu。两个没有同等的方法,但我没有使用它们,所以这里没有问题......
I'm desperately trying to enable the multiple-property to users of my webside. User should be able to upload a bunch of PDF-files to my server without choosing each file separately. I'm using gwt-upload for my uploads. Sadly the
MultiUploader
does not actually lets the user select several files at once, but you have to click the "Browse"-Button for every file again and select in in the browser.
I researched and realised that I need to have something like this
<input name='uploads[]' type="file" multiple>
Setting the "multiple"-property at the input created from gwt-upload does the trick so far that the opening file-dialog lets me select several files, but then the upload does not start / work anymore.
I could not find any example using a multi-file-input with gwt. It would be perfect if there is a way to do this with gwt-upload because I do not really want to implement the whole upload logic by myself again!
Thanks a lot!
解决方案 The solution is pretty simple. gwt-upload has a class Uploader which is able to do multiple uploads. The servlet code suggested in the wiki of gwt-upload is already capable of handling multiple uploads.
I had to change somethings in the Uploader class (source is fully available, so I just copied it and replaced the parts I needed). To access filenames and filesizes of the selected files I created a simple native method:
private static native String getFilesSelected() /*-{
var count = $wnd.$('input:file')[0].files.length;
var out = "";
for (i = 0; i < count; i++) {
var file = $wnd.$('input:file')[0].files[i];
out += file.name + ';' + file.size + ";";
}
return out;
}-*/;
whichs return value I split by ; to get the needed results.
And you need to replace the uploaders FileInput (setFileInput()) with a custom one, which sets the multiple property to the input. I use a wrapper class like this:
public class MyFileInput implements IFileInput {
private final FileUpload fu;
public MyFileInput() {
fu = new FileUpload();
DOM.setElementProperty(fu.getElement(), "multiple", "multiple");
}
}
you obviously need to implement the other methods of IFileInput, I linked them all through to fu. Two had no equivalent method, but I dont use them, so no problem here ...
这篇关于使用GWT一次上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!