Hi. Is there a way to measure the transferred size...
# ktor
a
Hi. Is there a way to measure the transferred size of a static file at the end of the request? For example, we have a 2GB static file. A user requests to get the file but cancels it when it's 25% completed. How to get the size which is 512MB?
a
Here is an example where total bytes is printed when downloading of a file is canceled by the client:
Copy code
val file = File("/path/to/file")
val channel = file.readChannel()

embeddedServer(Netty, port = 7070) {
    routing {
        get("/") {
            try {
                val content = object : OutgoingContent.ReadChannelContent() {
                    override fun readFrom(): ByteReadChannel = channel
                    override val contentType: ContentType = ContentType.defaultForFile(file)
                }

                call.respond(content)
            } catch (e: ChannelWriteException) {
                println(channel.totalBytesRead)
            }
        }
    }
}.start(wait = true)
👍 1