Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloaded filename is not respected #203

Closed
mshabarov opened this issue Feb 25, 2022 · 0 comments · Fixed by #209
Closed

Downloaded filename is not respected #203

mshabarov opened this issue Feb 25, 2022 · 0 comments · Fixed by #209
Assignees

Comments

@mshabarov
Copy link
Contributor

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

@mcollovati mcollovati self-assigned this Mar 4, 2022
mcollovati added a commit that referenced this issue Mar 7, 2022
Copy all headers set on a StreamResource to VaadinResponse before
writing contents.

Fixes #203
mshabarov pushed a commit that referenced this issue Mar 8, 2022
Copy all headers set on a StreamResource to VaadinResponse before
writing contents.

Fixes #203
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants