Vaadin 12: Upload Component does not trigger any Upload Events

Hey,

I’ve recently updated from Vaadin 11.0.1 to 12.0.0. Now ([the already flawed]
(https://vaadin.com/forum/thread/17336034/17378848)) Upload component ceased to work for me as I won’t receive any events connected to the upload of a file (like started, failed, succeeded). The listeners added with addXyzListener() are not triggered anymore. Anyone else having this issue? What could I try to do to get the listeners to work again?

Thanks in advance!

Here is my slightly customized Upload component:

public class PDEUpload extends Upload {
	
	/**
	 * @author bender
	 *
	 */
	@DomEvent("file-remove")
    public static class FileRemoveEvent extends ComponentEvent<Upload> {
		
		private static final long serialVersionUID = 2608319952827964746L;

		/**
		 * @param source
		 * @param fromClient
		 */
		public FileRemoveEvent(final Upload source, final boolean fromClient) {
			 super(source, fromClient);
        }
    }

	private static final long serialVersionUID = 4432689815466357962L;

	/**
	 * Constr.
	 */
	public PDEUpload() {
		super();
	}

	/**
	 * @param receiver
	 */
	public PDEUpload(final Receiver receiver) {
		super(receiver);
	}

	/**
	 * @param listener
	 * @return Registration
	 */
	public Registration addFileRemoveListener(final ComponentEventListener<FileRemoveEvent> listener) {
		return super.addListener(FileRemoveEvent.class, listener);
    }
}

The method which creates the custom Upload component:

protected Upload createUpload(final PDEMultiFileMemoryBuffer buffer) {
	final PDEUpload up = new PDEUpload(buffer);
	up.setAcceptedFileTypes(".aml", ".xml");
	up.addStartedListener(e -> Notification.show("Upload started.", 5000, Position.BOTTOM_END)); 					// Doesn't trigger.
	up.addFailedListener(e -> Notification.show("Upload failed.", 5000, Position.BOTTOM_END));						// Doesn't trigger.
	up.addSucceededListener(event -> {																			
		update();
		Notification.show(String.format("AML-file loaded: %s", event.getFileName()), 5000, Position.BOTTOM_END);	// Doesn't trigger.
	});
	up.addFileRemoveListener(event -> clear()); 																	// Triggers, but is my custom event anyway.
	
	return up;
}

The custom memory buffer:

public class PDEMultiFileMemoryBuffer implements MultiFileReceiver {

	private static final long serialVersionUID = 1321199489872872281L;
	
	private Map<String, FileData> files = new HashMap<>();

	/**
	 * Constr.
	 */
	public PDEMultiFileMemoryBuffer() {
		super();
	}	

    /**
     * (non-Javadoc)
     * @see com.vaadin.flow.component.upload.Receiver#receiveUpload(java.lang.String, java.lang.String)
     */
    @Override
    public OutputStream receiveUpload(final String fileName, final String MIMEType) {
    	final OutputStream outputBuffer = new ByteArrayOutputStream();
        files.put(fileName, new FileData(fileName, MIMEType, outputBuffer));

        return outputBuffer;
    }

    /**
     * Get the files in memory for this buffer.
     * 
     * @return files in memory
     */
    public Set<String> getFiles() {
        return files.keySet();
    }

    /**
     * Get file data for upload with file name
     * 
     * @param fileName
     *            file name to get upload data for
     * @return file data for filename or null if not found
     */
    public FileData getFileData(final String fileName) {
        return files.get(fileName);
    }

    /**
     * Get the output stream for file.
     * 
     * @param fileName
     *            name of file to get stream for
     * @return file output stream or empty stream if no file found
     */
    public ByteArrayOutputStream getOutputBuffer(final String fileName) {
        if (files.containsKey(fileName)) {
            return (ByteArrayOutputStream) files.get(fileName)
                    .getOutputBuffer();
        }
        
        return new ByteArrayOutputStream();
    }

    /**
     * Get the input stream for file with filename.
     * 
     * @param filename
     *            name of file to get input stream for
     * @return input stream for file or empty stream if file not found
     */
    public InputStream getInputStream(final String filename) {
        if (files.containsKey(filename)) {
            return new ByteArrayInputStream(((ByteArrayOutputStream) files
                    .get(filename).getOutputBuffer()).toByteArray());
        }
        
        return new ByteArrayInputStream(new byte[0]
);
    }
    
    /**
     * @return True if not empty
     */
    public boolean hasFiles() {
    	return !files.isEmpty();
    }
    
    /**
     * @param key
     */
    public void remove(final String key) {
    	files.remove(key);
    }
    
    /**
     * Clear Buffer
     */
    public void clear() {
    	files.clear();
    }
}

Hi,

if you can make a sample project that reproduces the issue. you should make a ticket at https://github.com/vaadin/vaadin-upload-flow/issues

Best regards,

+1 I come with the same issue
https://github.com/vaadin/vaadin-upload/issues/294

Olli Tietäväinen:
Hi,

if you can make a sample project that reproduces the issue. you should make a ticket at https://github.com/vaadin/vaadin-upload-flow/issues

Best regards,

Hey, thanks for your reply. Is what 来 周 provided in the now moved ticket https://github.com/vaadin/vaadin-upload-flow/issues/104 sufficient for you or do you need me to prepare an additional example? Also, not sure if that’s relevant, but I use Vaadin in combination with Spring Boot (Version 2.0.5.RELEASE).

It wouldn’t hurt if there was a minimal standalone application sample that demonstrates the issue. It’s good to mention the Spring Boot dependency, it may well have something to do with it. If you can reproduce the problem without Spring, even better.

I’ve attached an example Eclipse project to the ticket using Spring Boot and Vaadin 12. The error was reproducible on my end. If you point me to a tutorial that explains to me how I can create the same runnable sample without Spring Boot, I’d be happy to do so. Unfortunately, I’m not very firm with web stuff (yet) which is why I’ve opted for Spring and Vaadin in the first place.

Based on the ticket, it looks to be (possibly) a Spring issue indeed. https://github.com/vaadin/vaadin-upload-flow/issues/103 has more discussion, as does https://github.com/vaadin/spring/issues/381 .

Thanks for the input. I followed jessyZu’s advice in the ticket and set this value in my application.properties:

spring.servlet.multipart.enabled=false

It solves this particular issue for me.

The setting of spring.servlet.multipart.enabled=false in the application properties has made the Upload work for me too, but only for the first file. After the first upload, the successListener is no longer invoked anymore. Is anyone experiencing this too?

I am using Vaadin 13 and I set the spring.servlet.multipart.enabled=false and I get all the notification for each of the file I upload.

Upload upload = new Upload(new MultiFileBuffer());
upload.addStartedListener(e -> Notification.show("Upload started for file "+e.getFileName(), 4000, Position.BOTTOM_STRETCH));
upload.addProgressListener(e -> Notification.show("Upload progress "+e.getReadBytes(), 4000, Position.BOTTOM_STRETCH));
upload.addFinishedListener(e -> Notification.show("Upload finished for file "+e.getFileName(), 4000, Position.BOTTOM_STRETCH));
upload.addFailedListener(e -> Notification.show("Upload failed for file "+e.getFileName(), 4000, Position.BOTTOM_STRETCH));
upload.addSucceededListener(e -> Notification.show("Upload succeeded for file "+e.getFileName(), 4000, Position.BOTTOM_STRETCH));