我正在尝试使用Akka-http向浏览器发送较大/通常较慢的响应,以使其呈现为excel文件: 即:
$http({
method : "post",
url: "myUrl",
data: "large amount of request data to run a api call",
headers: {
'Content-type': 'application/json',
'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
},
responseType: 'arraybuffer'
}).then
(function mySuccess(response) {
var blob = new Blob([response.data], type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
savesAs(blob, "excel.xlsx")
})
运行缓慢的api,我尝试将数据分块,即:
path(myPath) {
post {
entity(as[MyPredicates]) { entity =>
val slowApi = Future[List[Results]] = runSlowApi(entity)
val excel = slowApi.map(data => generateExcel(data)).toByteArray
val source = Source.fromFuture(excel).map(ByteString.apply)
val chunkStream = source.via(new Chunker(chunkSize = 8192))
chunkStream.keepAlive(1.second, () => TextMessage.Strict("ping"))
complete(HttpEntity(MediaTypes.`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`, chunkStream)
}
}
对于少于60秒的任何东西,它都可以正常工作,但是60秒之后,它总是失败并显示
akka-http net :: err_incomplete_chunked_encoding 200(确定)
在浏览器上。
我尝试过withRequestTimeOut
/ withRequestTimeouResponse
/ withoutSizeLimit
并在客户端上设置超时,但没有任何效果吗?
答案 0 :(得分:0)
如果代码使用responseType: 'blob'
,效果会更好:
$http({
method : "post",
url: "myUrl",
data: "large amount of request data to run a api call",
headers: {
'Content-type': 'application/json',
'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
},
̶r̶e̶s̶p̶o̶n̶s̶e̶T̶y̶p̶e̶:̶ ̶'̶a̶r̶r̶a̶y̶b̶u̶f̶f̶e̶r̶'̶
responseType: 'blob'
}).then
(function mySuccess(response) {
̶v̶a̶r̶ ̶b̶l̶o̶b̶ ̶=̶ ̶n̶e̶w̶ ̶B̶l̶o̶b̶(̶[̶r̶e̶s̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶]̶,̶ ̶t̶y̶p̶e̶:̶ ̶'̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶v̶n̶d̶.̶o̶p̶e̶n̶x̶m̶l̶f̶o̶r̶m̶a̶t̶s̶-̶o̶f̶f̶i̶c̶e̶d̶o̶c̶u̶m̶e̶n̶t̶.̶s̶p̶r̶e̶a̶d̶s̶h̶e̶e̶t̶m̶l̶.̶s̶h̶e̶e̶t̶'̶)̶;̶
var blob = response.data;
savesAs(blob, "excel.xlsx")
})