简体   繁体   中英

How to show streams inline as HTTP chunked response with play framework

Am following the " Streaming HTTP Response " documentation of Play2! framework which describes how a file or stream could be sent as chunked responses. The http-action code I wrote after reading this is very simple -

Ok.chunked(
  Enumerator.fromStream(istream).andThen(Enumerator.eof)
)

With this code everytime I refresh the URL the data in the stream gets downloaded as a file by the browser. Instead of downloading as a file I want the stream content to shown inline in the browser as text. The File example on the documentation page describes how one could do this with files... but looking at the APIs I dont see an inline option with streams. So is it possible to show stream data inline with chunked responses everytime I refresh the browser? If my expectation is invalid then a little explanation of why-so will be very welcome.

From my comment: You should set a content-type supported by your browser (like text/plain or text/xml) when sending the response, otherwise you're just sending bytes and the browser doesn't "know" it can display it.

Update: adding the exact code that solved the issue:

Ok.chunked( Enumerator.fromStream(istream).andThen(Enumerator.eof) ).as("text/html")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM