Folder upload and preserve structure

I just updated my code base to the latest vaadin release which works great! I only have one question to see whether it is possible or not and that is if someone uses the upload function and uploads a folder. Will the API be able to tell me the nested folder structure? Let’s say a file 3 levels deep from the folder that was dragged into it /x/y/z/filename.txt that I know its ./x/y/z/filename.txt and not just filename.txt?

Nope according to:

  • The folder structure would not be preserved, the name for each uploaded file will just contain the file name, but not the folder hierarchy

Thanks for the swift reply! That’s a real bummer but it seems they are still working on this…?

Well… with no real change since last year I doubt that’s going to change in the next months to be honest without external interest / contribution / motivation

There’s an add-on that you can use for that case, Directory Upload. This add-on preserves the folder structure.

Diving into it! Looks very promising

Do you know if this supports the streaming of the content so that I don’t upload it first to the server?

As mentioned in Uploads and downloads, inputs and outputs | Vaadin

public class UploadFileHandlerExample extends VerticalLayout {

    public UploadFileHandlerExample() {
        UploadFileHandler uploadFileHandler = new UploadFileHandler(
                (InputStream content, String fileName, String mimeType)-> {
            try {
                int b = 0;

IIRC we discussed with Paola about enhancing that to that direction, but don’t know if that really happened :thinking: This thread inspired me to tackle one of my TODOs in UploadFileHandler in Viritin. Now, it also provides the “folderPath” if you drag in the folder on top of the Upload. Should be available in 2.17.3 soonish.

I hope we can get this kind of handy features now to core Upload as well after the recent refactoring. But I guess there is couple of more important issues to tackle before that so I wouldn’t hold my breath…

2 Likes

Sorry I missed your question. Directory Upload it supports streaming via the standard UploadFileHandler. It also supports the auto upload feature from the Upload component.

Thanks Matti,

Changed the call function and it works like a charm! Now people can mass upload and preserve the folder structure :slight_smile: . Next drink is on me!

1 Like

Thanks, definitely worth investigating! I was already using most of Matti’s example from the blog so it was a tiny change to get it working but good to know streaming is available :slight_smile:

1 Like

Checked the code and as it extends the Upload directly it might indeed work fine with the new UploadFileHandler when used on Vaadin 24.8 (didn’t try) :+1: Quite cool that we can improve add-ons by changing the framework :sunglasses:

Note that with Vaadin 24.8 there is still the “known issue” that on Spring Boot files are first streamed to a temp file and only after that from the temp file to the Vaadin handler. So you wont be really handling data while it is beeing uploaded to the server.

I made a tiny fix the meta data sending in the Viritin upload, now the “folder path” is custom character encoding safe. Cut a new minor 2.18.0 and also added option to make the OS dialog pick foldera instead of files (browser differences apparently on how they react to this non-standard flag).

Online demo and bit cleaner usage example (This streams properly without extra memory or saving files temporarily to server disk)

To prevent it to upload to the server first what would be the best solution? Switch to jetty or something else? Still a bit new in this field…

Until the bug (although one can naturally claim it is a feature :crazy_face:) in Vaadin is fixed, either use the UploadFileHandler from the Flow Viritin add-on or don’t use Spring Boot.

1 Like