使用 Kotlin 在 Vaadin 网格中下载组件
Download component in Vaadin grid with Kotlin
我想使用 Kotlin 和 Vaadin 14 在网格中提供下载按钮:
grid.addComponentColumn{item ->
val button = Button(item.plandatei)
val resource = StreamResource(item.plandatei) { this.getStream(item.plandatei) }
val anchor = Anchor(resource)
anchor.getElement().setAttribute("download", true)
anchor.getElement().appendChild(button.getElement())
return@addComponentColumn anchor
}
fun getStream(filename: String): InputStream {
val file = File(filename)
file.readBytes()
}
我收到此错误消息:
Overload resolution ambiguity. All these functions match.
public constructor StreamResource(name: String!, factory: InputStreamFactory!) defined in
com.vaadin.flow.server.StreamResource
public constructor StreamResource(name: String!, writer: StreamResourceWriter!) defined in
com.vaadin.flow.server.StreamResource
如何将 getStream 函数中的 InputStream 转换为 InputStreamFactory?还是其他原因?
您需要在那里帮助 Kotlin 编译器。请尝试
val resource = StreamResource(item.plandatei, InputStreamFactory { this.getStream(item.plandatei) } )
我想使用 Kotlin 和 Vaadin 14 在网格中提供下载按钮:
grid.addComponentColumn{item ->
val button = Button(item.plandatei)
val resource = StreamResource(item.plandatei) { this.getStream(item.plandatei) }
val anchor = Anchor(resource)
anchor.getElement().setAttribute("download", true)
anchor.getElement().appendChild(button.getElement())
return@addComponentColumn anchor
}
fun getStream(filename: String): InputStream {
val file = File(filename)
file.readBytes()
}
我收到此错误消息:
Overload resolution ambiguity. All these functions match.
public constructor StreamResource(name: String!, factory: InputStreamFactory!) defined in
com.vaadin.flow.server.StreamResource
public constructor StreamResource(name: String!, writer: StreamResourceWriter!) defined in
com.vaadin.flow.server.StreamResource
如何将 getStream 函数中的 InputStream 转换为 InputStreamFactory?还是其他原因?
您需要在那里帮助 Kotlin 编译器。请尝试
val resource = StreamResource(item.plandatei, InputStreamFactory { this.getStream(item.plandatei) } )