Vaadin 8 - 具有字符串内容的文本文件的下载按钮
Vaadin 8 - Download Button for a text file with String content
我想将 String
内容分配给一个变量,并允许用户将该字符串下载为文本文件。
我知道我可以使用来自 Vaadin 8 的 FileDownloader
但我不想要静态 file/image 下载功能而是 动态字符串内容 可以下载 浏览器中的文本文件。
这是 Scala 中的一个简单解决方案。我们创建了一个扩展 VerticalLayout 的 ReportDownloader
组件。它包含一个 "Download" 按钮,可在浏览器中触发文本文件下载。
import com.vaadin.ui.{Button, VerticalLayout}
import com.vaadin.server._
import java.io.{ByteArrayInputStream}
class VaadinStringStream(str : String, browserFileNameSuggestion : String) extends ConnectorResource {
override def getMIMEType: String = "text/plain"
override def getFilename: String = browserFileNameSuggestion
override def getStream: DownloadStream = new DownloadStream(
new ByteArrayInputStream(str.getBytes), getMIMEType, getFilename
)
}
class ReportDownloader extends VerticalLayout{
val btn = new Button("DOWNLOAD")
this addComponent btn
val stream = new VaadinStringStream("This is a sample txt file content", "report.txt")
new FileDownloader(stream).extend(btn)
}
VaadinStringStream
将动态字符串内容和浏览器建议的文件名作为参数。
FileDownloader
使用的Vaadin中的资源接口有点麻烦。尤其是对于动态创建的内容,您希望推迟其生成直到用户实际单击下载按钮,这在典型的 Vaadin 应用程序中非常常见。
Viritin 附加组件
因此,我建议将 Viritin add-on 添加到您的申请中。使用其中的 DownloadButton
组件,这大大简化了用法。
这是一个简单的例子:
DownloadButton simple = new DownloadButton(out -> {
try {
out.write("Foobar".getBytes());
} catch (IOException ex) {
// exception handling
}
}).withCaption("Simple Download");
要查看更完整的示例,请参阅 Viritin 项目中的 the tests。如果您仔细阅读 DownloadButton
class.
的源代码,您还可以从项目中看到 "raw solution"
PS。我是 Viritin 的作者,所以有点偏向于建议它的用法,但我也为 Vaadin Ltd 与 Vaadin 合作了十多年。
我想将 String
内容分配给一个变量,并允许用户将该字符串下载为文本文件。
我知道我可以使用来自 Vaadin 8 的 FileDownloader
但我不想要静态 file/image 下载功能而是 动态字符串内容 可以下载 浏览器中的文本文件。
这是 Scala 中的一个简单解决方案。我们创建了一个扩展 VerticalLayout 的 ReportDownloader
组件。它包含一个 "Download" 按钮,可在浏览器中触发文本文件下载。
import com.vaadin.ui.{Button, VerticalLayout}
import com.vaadin.server._
import java.io.{ByteArrayInputStream}
class VaadinStringStream(str : String, browserFileNameSuggestion : String) extends ConnectorResource {
override def getMIMEType: String = "text/plain"
override def getFilename: String = browserFileNameSuggestion
override def getStream: DownloadStream = new DownloadStream(
new ByteArrayInputStream(str.getBytes), getMIMEType, getFilename
)
}
class ReportDownloader extends VerticalLayout{
val btn = new Button("DOWNLOAD")
this addComponent btn
val stream = new VaadinStringStream("This is a sample txt file content", "report.txt")
new FileDownloader(stream).extend(btn)
}
VaadinStringStream
将动态字符串内容和浏览器建议的文件名作为参数。
FileDownloader
使用的Vaadin中的资源接口有点麻烦。尤其是对于动态创建的内容,您希望推迟其生成直到用户实际单击下载按钮,这在典型的 Vaadin 应用程序中非常常见。
Viritin 附加组件
因此,我建议将 Viritin add-on 添加到您的申请中。使用其中的 DownloadButton
组件,这大大简化了用法。
这是一个简单的例子:
DownloadButton simple = new DownloadButton(out -> {
try {
out.write("Foobar".getBytes());
} catch (IOException ex) {
// exception handling
}
}).withCaption("Simple Download");
要查看更完整的示例,请参阅 Viritin 项目中的 the tests。如果您仔细阅读 DownloadButton
class.
PS。我是 Viritin 的作者,所以有点偏向于建议它的用法,但我也为 Vaadin Ltd 与 Vaadin 合作了十多年。