Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Die Upload-Komponente ermöglicht den Zugriff auf das lokale Dateisystem des Client, um beliebige Dateien auszuwählen und anschließend auf den Server hochzuladen.

...


Code Block
languagejava
themeConfluence
public class MainView extends XdevView {

	File file;

	public MainView() {
		super();
		this.initUI();

		final Receiver receiver = new Receiver() {

		    @Override
		    public OutputStream receiveUpload(final String filename, final String mimeType) {
		        // Create upload stream to write to

		        FileOutputStream fos = null;
		        try {

		            // Get path to servlet's temp directory
		            final File temporaryDirectory = (File) VaadinServlet.getCurrent().getServletContext().getAttribute(ServletContext.TEMPDIR);

		            // Concatenate temporaryDirectory with filename and open the file for writing.
		            MainView.this.file = new File(temporaryDirectory, filename);

		            // Create the output stream
		            fos = new FileOutputStream(MainView.this.file);

		        } catch (final java.io.FileNotFoundException e) {
		            Notification.show("Could not open file", Type.ERROR_MESSAGE);
		            return null;
		        }
		        return fos;
		    }
		};

		this.upload.setReceiver(receiver);
	}

// Set Receiver for upload component
		this.upload.setReceiver(receiver);
	}