簡體   English   中英

Vaadin 上傳不適用於相同的文件名

[英]Vaadin upload not working for same filename

我正在嘗試使用 Vaadin 上傳上傳 .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();
    }

}

發生這種情況是因為目標輸入的值未更改,因此未觸發更改事件。 它是 Chrome/Chromium 中的一個錯誤。

Vaadin 8.3 和更新版本中有一個修復程序來解決這個問題(參見https://github.com/vaadin/framework/issues/9635 )。

如果您使用的是舊版本的框架,請使用下面的 function 手動重置目標 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 文件輸入字段的名稱屬性來調用此 function。 您可以通過檢查元素工具找到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM