vaadin FileDownloader重置扩展名

时间:2019-02-05 13:24:12

标签: java vaadin8

我们知道,我们必须用按钮扩展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:该连接器不是给定扩展名的父级       在com.vaadin.server.AbstractClientConnector.removeExtension(AbstractClientConnector.java:595)

fileDownloader.removeExtension(downloadButton); 

在这里,我们不能将Button应用于扩展

如何重置FileDownloader的Button?

1 个答案:

答案 0 :(得分:0)

您扩展下载范围

    fileDownloader.extend(download);

但是尝试从fileDownloader中删除扩展名

 downloadButton.removeExtension(fileDownloader);

那是不匹配的。 (尽管这是一个错字。)

您可以删除按钮,然后创建一个新的Button,一个新的下载器,然后进行扩展。但是某些扩展无法删除。

但是,您不需要。您可以只更新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");
      }
    }