我们知道,我们必须与按钮延长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异常:该连接器是不适合在com.vaadin.server.AbstractClientConnector.removeExtension给定延伸的父(AbstractClientConnector.java:595)
fileDownloader.removeExtension(downloadButton);
在这里,我们不能Apply按钮,扩展
如何重置FileDownloader
的按钮?
您扩展下载
fileDownloader.extend(download);
但尝试删除从fileDownloader扩展
downloadButton.removeExtension(fileDownloader);
这是一个不匹配。 (假设是一个错字虽然。)
您可以删除按钮,然后创建一个新的按钮,新的下载,然后扩展这一点。有些分机不能被删除,虽然。
但是,你并不需要这一点。您只需更新StreamResource并没有触及绑定的。
一个更复杂的例子是从https://vaadin.com/docs/v8/framework/articles/LettingTheUserDownloadAFile.html的OnDemandDownloader
/**
* 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");
}
}