在Vaadin Flow应用程序中以默认为用户事件日期时间的文件名下载 [英] Download with file name defaulting to date-time of user event in Vaadin Flow app

查看:68
本文介绍了在Vaadin Flow应用程序中以默认为用户事件日期时间的文件名下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的

也许解决方案是使用 Button 小部件而不是 Anchor .高级主题> 解决方案

可以使用Vaadin Flow中的按钮来启动下载吗?

是的,但是需要一些客户端实现.目录中有文件下载包装插件.这样就可以包装例如按钮.但是我认为这不能完全解决您的问题.我怀疑在点击事件中设置文件名不会适用(为时已晚).但我确实认为,可以在此加载项中添加文件名提供程序回调功能.

In my Vaadin Flow web app (version 14 or later), I want to present to my user a link that will download a data file to them. The default name of the file to be downloaded should be determined at the moment the user initiates the download.

I am aware of the Anchor widget in Vaadin Flow. With an Anchor, the default name for the downloaded file will be the resource name given in the URL of the link. Unfortunately, that is determined when the page loads rather than later when the user clicks the link. So this approach fails to meet my need to label the download with the date-time captured at the moment the user initiates the download.

String when = Instant.now().toString().replace( "-" , "" ).replace( ":" , "" ); // Use "basic" version of standard ISO 8601 format for date-time.
StreamResource streamResource = new StreamResource( "rows_" + when + ".txt" , ( ) -> this.makeContent() );
Anchor anchor = new Anchor( streamResource , "Download generated data" );

Perhaps the solution would be the use of a Button widget rather than an Anchor. Using a button for dynamically-created content is shown in the manual in the Advanced Topics > Dynamic Content page. Unfortunately, the example there loads a resource on the page rather than doing a download for the user.

➥ Can a Button in Vaadin Flow be used to initiate a download?

➥ Is there some other approach to initiating a download with a URL determined when the user initiates the download rather than when the page loads?

解决方案

Can a Button in Vaadin Flow be used to initiate a download?

Sort of yes, but it requires some client side implementation. There is File Download Wrapper add-on in Directory, which does this. With it is possible to wrap e.g. Button. However I think it will not solve problem of yours fully. I suspect that the setting the filename in click event wont apply (it comes too late). But I do think, that it would be possible to add filename provider callback feature to this add-on.

这篇关于在Vaadin Flow应用程序中以默认为用户事件日期时间的文件名下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆