A your linked SO thread explains, it is not possible to restrain files by their name in the front-end, only by their file type. An additional restraint needs to be implemented in the back-end aka after they tried to upload it.
you could for example check the filename of the uploaded file inside the succeededListener of the Upload component
// these two are frontend restrictions - no round trip to the server needed for max files and file type
upload.setMaxFiles(UPLOAD_MAX_FILES);
upload.setAcceptedFileTypes(".txt");
// this is a backend restriction - user can select "invalid" files to upload (as long as all frontend restrictions are valid), and after a server roundtrip we tell the user he cannot do that
upload.setSucceededListener(event -> {
if(!event.getFileName().equals("text")){
Notification.show("You can only uplaod files with the name 'text.txt'");
} else {
// do stuff with the uploaded file, which is called "text.txt"
}
}
It is also good practice to let the user know of such a file name restiction beforehand.
Kaspar Scherrer:
A your linked SO thread explains, it is not possible to restrain files by their name in the front-end, only by their file type. An additional restraint needs to be implemented in the back-end aka after they tried to upload it.
you could for example check the filename of the uploaded file inside the succeededListener of the Upload component
// these two are frontend restrictions - no round trip to the server needed for max files and file type
upload.setMaxFiles(UPLOAD_MAX_FILES);
upload.setAcceptedFileTypes(".txt");
// this is a backend restriction - user can select "invalid" files to upload (as long as all frontend restrictions are valid), and after a server roundtrip we tell the user he cannot do that
upload.setSucceededListener(event -> {
if(!event.getFileName().equals("text")){
Notification.show("You can only uplaod files with the name 'text.txt'");
} else {
// do stuff with the uploaded file, which is called "text.txt"
}
}
It is also good practice to let the user know of such a file name restiction beforehand.
Thank you for reply, i just re-read my linked thread and… yes, it impossible to do that
I am doing the same way as your example. Thank you for help