簡體   English   中英

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); 

在這里我們不能將按鈕應用到擴展

如何重置按鈕的FileDownloader

您擴展下載

    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");
      }
    }

你可以做一個小技巧:

downloadButton.removeExtension(downloadButton.getExtensions().iterator().next());

當我嘗試向按鈕添加不同的新下載並且我想刪除舊下載時,這對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM