的解決方案,第二次點擊需要下載的文件是按鈕的監聽器。就像我的情況,我有點擊監聽器的按鈕,其中FileDownloader擴展按鈕。但它應該沒有監聽器,因爲FileDownloader有自己的機制來處理監聽器操作。
在這裏,第一個按鈕點擊被clickListener處理,只有這樣,fileDownloader擴展了下載按鈕,該按鈕包含下載文件的所有功能,並且只有當單擊事件通過FileDownloader時纔會發生此功能。所以下次單擊FileDownloader時會像現在這樣擴展按鈕。
public static Button getDownloadButton(String fileName, String fileAsString, String caption) {
// caption
Button dwnButton = new Button(caption, VaadinIcons.DOWNLOAD);
dwnButton.addClickListener(listener -> {
StreamResource resource = createResource(fileName, fileAsString);
FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(dwnButton);
});
return dwnButton;
}
在這裏,fileDownloader已經擴展了按鈕,它具有所有的流資源。所以在第一次點擊時只下載被調用。
public static Button getDownloadButton(String fileName, String fileAsString, String caption) {
// caption
Button dwnButton = new Button(caption, VaadinIcons.DOWNLOAD);
StreamResource resource = createResource(fileName, fileAsString);
FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(dwnButton);
return dwnButton;
}
你可以添加在其中創建您的按鈕的代碼? –
我記得前一陣子,在按鈕點擊偵聽器中設置流資源時發生了這種情況。這是你的情況嗎? – Morfic
我也有同樣的錯誤。我想我記得在下載事件之後流正在設置*,這就是爲什麼您需要再次點擊。這可以測試更改您點擊後下載的文件。 – Shirkam