【问题标题】:GWT - Upload and get files uploaded with FileUploadGWT - 使用 FileUpload 上传和获取文件
【发布时间】:2021-08-10 00:32:34
【问题描述】:

我正在使用 FileUpload 类来上传多个文件。由于我没有选择使用库(如 gwt-upload 或其他),我创建了 MultiFileUpload 类:

public class MultiFileUpload extends FileUpload {
    public MultiFileUpload() {
        this.getElement().setAttribute("multiple", "multiple");
        this.setTitle("Select files");
    }
}

我已将它添加到我的面板中,并且可以成功选择多个文件:

private MultiFileUpload upload = new MultiFileUpload();
upload.getElement().setId("files");
grid.setWidget(2, 1, upload);

我的问题是: 我无法获取每个文件的路径以将其发送到另一个服务(来自使用 Jave Entreprise Edition aka jee 创建的另一个模块,我应该在其中使用路径)。

我尝试了什么

  • 从 FileUpload 获取文件
upload.getElement.getgetChildCount(); ==> equal 0
upload.getFileName(); ==> null
  • 我试过用js:
private static native boolean validateFiles() /*-{
        var filesCount = $wnd.$('input:file')[0].files.length;
        for(i = 0; i<filesCount; i++){
        console.log($wnd.$('input:file')[0].files[i].path) //==> path is undefined
        }
}-*/;
  • 使用 DOM:
Element e =  DOM.getElementById("files");
e.getgetChildCount(); ==> equal 0

非常感谢任何帮助。

【问题讨论】:

    标签: file-upload gwt


    【解决方案1】:

    解决方案: 我在 POST 请求中向 servlet 发送文件和所有表单数据:

    form.setAction(UPLOAD_ACTION_URL);
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);
    

    然后在我的 servlet 中,我从请求中获取文件路径:

    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    
    Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
                FileItem item = iter.next();
                if (item.isFormField()) {
                    String fileName = new File(item.getName()).getName();
                    File uploadedFile = new File(fileName);
                    item.write(uploadedFile);
                  }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 1970-01-01
      • 2015-07-09
      • 2018-01-31
      • 1970-01-01
      • 2018-08-20
      • 2017-09-25
      • 2011-10-01
      • 1970-01-01
      相关资源
      最近更新 更多