Limit file upload components

I use the vaadin download component (7.1.9), now my problem is that I can not limit which files can be sent using the download component to the server, but I don’t know, t found an API for this purpose. The only way is to discard the file of the wrong types after downloading.

public OutputStream receiveUpload(String filename, String mimeType) {

    if(!checkIfAValidType(filename)){
        upload.interruptUpload();
    }          

    return out;
}

Is it correct?

+3
source share
3 answers

No, this is not the right way. The fact is that Vaadin provides many useful interfaces that you can use to monitor when a download has started, interrupted, finished or failed. Here is the list:

com.vaadin.ui.Upload.FailedListener;
com.vaadin.ui.Upload.FinishedListener;
com.vaadin.ui.Upload.ProgressListener;
com.vaadin.ui.Upload.Receiver;
com.vaadin.ui.Upload.StartedListener;

Here is a snippet of code that will give you an example:

@Override
public void uploadStarted(StartedEvent event) {
    // TODO Auto-generated method stub
    System.out.println("***Upload: uploadStarted()");

    String contentType = event.getMIMEType();
    boolean allowed = false;
    for(int i=0;i<allowedMimeTypes.size();i++){
        if(contentType.equalsIgnoreCase(allowedMimeTypes.get(i))){
            allowed = true;
            break;
        }
    }
    if(allowed){
        fileNameLabel.setValue(event.getFilename());
        progressBar.setValue(0f);
        progressBar.setVisible(true);
        cancelButton.setVisible(true);
        upload.setEnabled(false);
    }else{
        Notification.show("Error", "\nAllowed MIME: "+allowedMimeTypes, Type.ERROR_MESSAGE);
        upload.interruptUpload();
    }

}

Here allowMimeTypes is an array of strings of type mime.

ArrayList<String> allowedMimeTypes = new ArrayList<String>();
allowedMimeTypes.add("image/jpeg");
allowedMimeTypes.add("image/png");

Hope this helps you.

+7

.

, ( HTML 5, accept) - .csv :

upload.setButtonCaption("Import");
JavaScript.getCurrent().execute("document.getElementsByClassName('gwt-FileUpload')[0].setAttribute('accept', '.csv')");
+2

I think it is best to add a custom exception from Receiver's receiveUpload:

Upload upload = new Upload(null, new Upload.Receiver() {
    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        boolean typeSupported = /* do your check*/;
        if (!typeSupported) {
            throw new UnsupportedImageTypeException();
        }
        // continue returning correct stream
    }
});

The exception is just the usual exception:

public class UnsupportedImageTypeException extends RuntimeException {
}

Then you just add a listener if the download fails, and check if your exception is the cause:

upload.addFailedListener(new Upload.FailedListener() {
    @Override
    public void uploadFailed(Upload.FailedEvent event) {
        if (event.getReason() instanceof UnsupportedImageTypeException) {
            // do your stuff but probably don't log it as an error since it not 'real' error
            // better would be to show sth like a notification to inform your user
        } else {
            LOGGER.error("Upload failed, source={}, component={}", event.getSource(), event.getComponent());
        }
    }
});
0
source

All Articles