【问题标题】:Vaadin upload not working for same filenameVaadin 上传不适用于相同的文件名
【发布时间】:2016-08-19 09:46:39
【问题描述】:

我正在尝试使用 Vaadin Upload 上传 .xlsx 文件,当我再次上传同一个文件时,什么也没有发生,它也不会触发任何事件。第一次我可以上传文件,它工作得很好,但是之后当我再次尝试上传相同的文件时,什么也没有发生。我无法弄清楚这个问题。

uploader = new Upload(null, this);
uploader.setImmediate(true);
uploader.setButtonCaption("Upload Template");

uploader.addStartedListener(this);
uploader.addFinishedListener(this);

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
    uploadedFilename = filename;
    FileOutputStream fos = null; // Stream to write to
    try {

        String filepath = attachmentsTmpFolderLoc + File.separator + "Uploads";
        File folder = new File(filepath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        uploadFile = new File(filepath + File.separatorChar + filename);
        fos = new FileOutputStream(uploadFile);

    } catch (Exception e) {
        e.printStackTrace();
        return new NullOutputStream();
    }
    return fos;
}

@Override
public void uploadStarted(StartedEvent event) {
    uploader.setVisible(false);
}

@Override
public void uploadFinished(FinishedEvent event) {
    uploader.setVisible(true);
    getFinishedFile();


}

private void getFinishedFile() {
    String loginUserId = activeUserObject.getLoginId();
    String fileName = loginUserId + "_" + this.jobPkey + ".xlsx";

    if (!validatFileName(loginUserId, this.jobPkey)) {
        new IMCNotification().showError("Adressing - Wrong Template Name", "Please Upload file with Name : '" + fileName + "'.<BR> The uploaded file name should be the same as the downloaded file name.<BR> Please correct the file name and try again.");
        uploader.interruptUpload();

    } else {
        prepareCustomValidations();
    }

}

【问题讨论】:

  • 作为一种解决方法,我会在上传后重新创建上传组件。
  • 让我知道你是如何重现的。

标签: java file-upload vaadin vaadin8


【解决方案1】:

发生这种情况是因为目标输入的值没有改变,因此没有触发 change 事件。这是在 Chrome/Chromium 中看到的错误。

Vaadin 8.3 和更新版本中有一个修复程序来解决这个问题(请参阅https://github.com/vaadin/framework/issues/9635)。

如果您使用的是旧版本的框架,请使用以下函数手动重置目标 HTML 文件输入字段:

private void manuallyResetFileInput(String name) {
    final String js = String.format("for (let x of document.getElementsByName('%s')) if (x.type == 'file') x.value = '';", name);
    if (getUI() != null && getUI().getSession() != null) {
        getUI().access(() -> {
            Page.getCurrent().getJavaScript().execute(js);
        });
    } else {
        Page.getCurrent().getJavaScript().execute(js);
    }
}

您可能需要将 import com.vaadin.server.Page; 添加到您的导入中。

现在通过传递要重置的 HTML 文件输入字段的名称属性来调用此函数。您可以通过 Inspect Element 工具找到它。

【讨论】:

    猜你喜欢
    • 2014-01-02
    • 2015-05-30
    • 1970-01-01
    • 2012-08-02
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多