As StreamResource is depreciated, how to show pdf in filestorage in iframe

now in vaadin StreamResource is depreciated, how can i show pdf stored in filestorage in iframe. My present code is-

if (event.getItem() != null) {
            FileStorage fileStorage = fileStorageLocator.getDefault();
            FileRef document = event.getItem().getFile();
            if (document == null) {
                return;
            }

            try (InputStream inputStream = fileStorage.openStream(document)) {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputStream.readAllBytes());
                StreamResource resource = new StreamResource(document.getFileName(), () -> byteArrayInputStream);
                StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
                frame1.setSrc(registration.getResourceUri().toString());
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }

Hi,

According to the StreamResource JavaDoc, the com.vaadin.flow.server.streams.DownloadHandler should be used instead. Take a look at the Vaadin doc for more details.

Regards,
Gleb

Hi Gleb,

How about MemoryBuffer?

image

Use com.vaadin.flow.server.streams.UploadHandler instead?

Regards,
CK

Looks like it. Take a look at the Vaadin doc for more details.

Regards,
Gleb

hi @gorelov

I want to replace StreamResource with DownloadHandler to display an image in a component and the AI tells me

https://ai-assistant.jmix.io/chatbot?chatSessionId=019bc04c-f3e6-7e8b-b65d-3dffb3e09773

Given what’s in the official Jmix 2.x docs/samples:

There is no provided factory or example like
DownloadHandler.fromStream(() -> fileStorage.openStream(fileRef), ...) in Jmix samples.

Possible to adapt the documentation ( for example 6. Working with Images :: Jmix Documentation ) ?

Thank you
Felix

Hi @f.zehnder,

The linked doc section (Displaying Image in Form) doesn’t contain a sample code that uses StreamResource. Probably the link refers to the incorrect section. Anyway, I’ve created a github issue - Update sample code that uses deprecated StreamResource to use DownloadHandler · Issue #1096 · jmix-framework/jmix-docs · GitHub.

Indeed, DownloadHandler doesn’t provide the fromStream factory method, instead, it provides fromInputStream.

Regards,
Gleb

Hi @gorelov

Do I missinterpret something ?
image

Regards
Felix

The link you provided refers to the Displaying Image in Form section that doesn’t contain StreamResource usage. The code you’ve attached is from the Displaying Image in Data Grid section. Anyway, as I said, I’ve created a github issue.

Regards,
Gleb