I have an http url representing image/video/plain text or any file type. Just assume that it is a simple direct url. When I click the download button, the file should be downloaded from the url to the system with a dialog as in below snapshot.
File name and file type would be dynamic.
I do not want to open a new browser tab for this purpose as the best user experience is just to prompt the download straight away. How do I do this in Vaadin? My Vaadin version is 23.2.0
I’m thinking how would I write the button click listener to achieve this?
But the link href is still static. I have a text label and user input some text there. When the user click on download button, it is going to dynamically find the download url (based on the value entered in textfield) from which the file is downloaded. I’m afraid I can only go for button addClickListener in this case. At this point vaadin button has the click listener part, but only vaadin anchor has the direct download feature. Let me know if you understood the problem.
Based on a user-input (text label), I want to download from a dynamic url (button click event).
downloadButton.addClickListener(e -> {
String downloadUrl = findUrl(textField.getValue());
StreamResource resource = new StreamResource("file.jpg", () -> {
return new URL(downloadUrl).openStream();
});
//I have the download url ready, resource created
//But how can I get it downloaded in the next step?
// Seems like I can't achieve click listener in anchor
});
It too have a click listener, but ends up in the same situation as in normal button (where I create the resource dynamically, but don’t know how to download). How can I download from a dynamic url and prompt for download?
Above that code is everything you need, a callback once the button is clicked as well as mine type and file name provider to customize it depending on the value of your text field
I see it is an extension of button, but it is throwing errors on these existing methods and I was wondering how to achieve the same results (setting the label, theme variants, adding it to a layout etc)
buttonLayout.add(downloadButton); // buttonLayout is a horizontal vaadin layout
``
It is complaining that `DownloadButton` is not a component! It can't seem to work.
You have to do something wrong. Please check your imports, you can even see that it’s extending Vaadin’s Button class and because of that you can call any methods you normally do on a button.
@Route(value = “some-view”, layout = MainLayout.class)
public class SomeView extends Div {
DownLoadButton downLoadButton = new DownloadButton(out → {
out.write(“some data”);
} );
// add(downLoadButton) //this fails as well
HorizontalLayout buttonLayout = new HorizontalLayout ();
buttonLayout.add(downLoadButton) // this is failing
add(buttonLayout);
}
``
This is the main class that looks fine for me.
And I just import this: import org.vaadin.viritin.button.DownloadButton;