vaadin / portlet

Portlet support for Vaadin Flow
https://vaadin.com
Other
2 stars 3 forks source link

Downloaded filename is not respected #203

Closed mshabarov closed 2 years ago

mshabarov commented 2 years ago

Downloaded filename (including its extension) is not respected in Vaadin Portlet when running in Liferay 7. The following code aims to add a download button with excel file download action:

public class ContactListView extends VerticalLayout implements PortletView {

    private Collection<StreamRegistration> streamRegistrations;

    @Override
    public void onPortletViewContextInit(PortletViewContext context) {
        add(download());
    }

    @Override
    protected void onDetach(DetachEvent detachEvent) {
        if (streamRegistrations != null) {
            streamRegistrations.forEach(StreamRegistration::unregister);
            streamRegistrations.clear();
        }
        super.onDetach(detachEvent);
    }

    private Button download() {
        return new Button("Download File", click -> {
            InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("Book1.xlsx");
            StreamResource downloadResource = new StreamResource("export.xlsx", () -> resourceAsStream);
            downloadResource.setContentType("application/xls");
            downloadResource.setHeader("Content-Disposition",
                    "attachment;filename=export.xlsx");
            final StreamRegistration registration =
                    VaadinPortletSession.getCurrent().getResourceRegistry().registerResource(downloadResource);

            UI.getCurrent().getPage().open(registration.getResourceUri().toString());
            if (streamRegistrations == null) {
                streamRegistrations = new LinkedList<>();
            }
            streamRegistrations.add(registration);
        });
    }
}

Expected the filename is "export.xlsx", but actual filename is "home". Request URL = http://localhost:8080/web/guest/home?p_p_id=ContactList_WAR_addressbookgrid&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=/uidl&p_p_cacheability=cacheLevelPage&v-r=uidl&v-uiId=0