vaadin FileDownloader 重置扩展

vaadin FileDownloader reset extension

我们知道,我们必须用按钮扩展 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); 

这里我们不能将 Button 应用到 Extension

如何为按钮重置 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");
      }
    }