简体   繁体   中英

Vaadin upload not working for same filename

I am trying to upload.xlsx file using Vaadin Upload, When I am upload the same file again nothing is happening, it does not fire any events too. For first time I can upload file, and it works very fine, but after that when I am trying to upload the same file again, nothing happens. I could not figure out the issue.

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();
    }

}

This is happening because value of target input is not getting changed, hence not firing the change event. Its a bug seen in Chrome/Chromium.

There is a fix in Vaadin 8.3 and newer versions to overcome this problem (see https://github.com/vaadin/framework/issues/9635 ).

If you are using older version of the framework, use below function to manually reset the target HTML file input field:

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);
    }
}

You may have to add import com.vaadin.server.Page; to your imports.

Now call this function by passing name property of HTML file input field which you want to reset. You can find this via Inspect Element tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM