gpt4 book ai didi

java - vaadin FileDownloader 重置扩展

转载 作者:行者123 更新时间:2023-12-02 10:18:02 25 4
gpt4 key购买 nike

众所周知,我们必须使用按钮扩展 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 获取多个文件。我需要重置 Button 或 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,根本不接触绑定(bind)。

一个更详细的示例是 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");
}
}

关于java - vaadin FileDownloader 重置扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54535455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com