Amira
Amira

Reputation: 3274

Control Uploaded file types (only specific extension can be uploaded)

i have a stupid question here i'm implementing upload button with vaadin and i want the users to upload only compressed files (.zip,.rar..), imake a search but i didn't find something useful : so i tried to do this , i know it's not good solution because the user already uploaded the selected file :

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
   // Create upload stream
        FileOutputStream fos = null; // Stream to write to
        String fileName ;
        String userHome = System.getProperty( "user.home" );
        try {
            // Open the file for writing.
            file = new File(userHome+"/kopiMap/runtime/uploads/report/" + filename);
            fileName= file.getName();
                //Here i will get file extension
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            Notification.show(
                    "Could not open file<br/>", e.getMessage(),
                    Notification.TYPE_ERROR_MESSAGE);
            return null;
        }
        return fos; // Return the output stream to write to
    }

So how to do it before uploading

Upvotes: 0

Views: 2620

Answers (2)

DarioBB
DarioBB

Reputation: 663

You can add this and it will work (all done by HTML 5 and most browser support now accept attribute) - this is example for .csv files:

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

Upvotes: 1

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

you can check the mimeType and if it is application/zip

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
   // Create upload stream
if(mimeType.equals("application/zip"))
//Here you can restrict

Upvotes: 2

Related Questions