Как мы знаем, мы должны расширить FileDownloader с помощью кнопки, чтобы иметь возможность скачать файл.
//
Button downloadButton = new Button("download");
private void updateFileForDownload(){
...
StreamResource sr = getFileStream();
FileDownloader fileDownloader = new FileDownloader(sr);
fileDownloader.extend(downloadButton);
...
}
private StreamResource getFileStream() {
StreamResource.StreamSource source = () -> new ByteArrayInputStream(binderDocument.getBean().getFile());
StreamResource resource = new StreamResource(source, binderDocument.getBean().getFilename());
return resource;
}
У меня есть проблема в моем приложении. Если я вызову метод updateFileForDownload более одного раза, я получу более одного файла, нажав на downloadButton. Мне нужно расширение сброса для кнопки или для FileDownloader. Я пробовал оба:
downloadButton.removeExtension(fileDownloader);
Здесь мы получаем
java.lang.IllegalArgumentException: This connector is not the parent for given extension at com.vaadin.server.AbstractClientConnector.removeExtension(AbstractClientConnector.java:595)
fileDownloader.removeExtension(downloadButton);
и здесь мы не можем применить кнопку к расширению
Как я могу сбросить FileDownloader для кнопки?




Вы продлеваете загрузку
fileDownloader.extend(download);
но попробуй удалить расширение из fileDownloader
downloadButton.removeExtension(fileDownloader);
Это несоответствие. (Предположим, что это опечатка.)
Вы можете удалить кнопку, а затем создать новую кнопку, новый загрузчик, а затем расширить его. Однако некоторые расширения нельзя удалить.
Однако вам это не нужно. Вы можете просто обновить StreamResource и вообще не трогать привязку.
Более сложный пример — OnDemandDownloader из https://vaadin.com/docs/v8/framework/articles/LettingTheUserDownloadAFile.html.
/**
* This specializes {@link FileDownloader} in a way, such that both the file name and content can be determined
* on-demand, i.e. when the user has clicked the component.
*/
public class OnDemandFileDownloader extends FileDownloader {
/**
* Provide both the {@link StreamSource} and the filename in an on-demand way.
*/
public interface OnDemandStreamResource extends StreamSource {
String getFilename ();
}
private static final long serialVersionUID = 1L;
private final OnDemandStreamResource onDemandStreamResource;
public OnDemandFileDownloader (OnDemandStreamResource onDemandStreamResource) {
super(new StreamResource(onDemandStreamResource, ""));
this.onDemandStreamResource = checkNotNull(onDemandStreamResource,
"The given on-demand stream resource may never be null!");
}
@Override
public boolean handleConnectorRequest (VaadinRequest request, VaadinResponse response, String path)
throws IOException {
getResource().setFilename(onDemandStreamResource.getFilename());
return super.handleConnectorRequest(request, response, path);
}
private StreamResource getResource () {
return (StreamResource) this.getResource("dl");
}
}
я написал здесь код без IDE, и мне не удалось ввести имена, см. Соответствующие обновления