我在 Play for Scala 中有以下代码尝试将 Excel 文件发送到浏览器:
val out = new ByteArrayOutputStream
val exporter = new org.pivot4j.ui.poi.ExcelExporter(out)
val renderer = new TableRenderer
renderer.render(model, exporter);
out.flush
out.close
Ok(out).withHeaders(
CONTENT_TYPE -> "application/vnd.ms-excel",
CONTENT_DISPOSITION -> s"attachment; filename = file.xlsx")
Ok
抛出编译错误:
◾Cannot write an instance of java.io.ByteArrayOutputStream to HTTP response. Try to define a Writeable[java.io.ByteArrayOutputStream]
尽管ByteArrayOutputStream
是输出类型,但无法写入。如何解决这个问题?
最佳答案
尝试在 out
上调用 toByteArray
,如下所示:
Ok(out.toByteArray).withHeaders(...)
DefaultWritables提供 Play 开箱即用提供的 Writable
列表。特别是,存在 Writeable[Array[Byte]]当您调用 toByteArray 时应该会获取它
关于scala - 无法在 Play for Scala 中发送到浏览器 ByteArrayOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50765602/