is it possible to listen to the progress it takes ...
# ktor
a
is it possible to listen to the progress it takes to receive a multipart file in a ktor server? I have a ktor server running on android and I would like to display a progress bar for files being uploaded to the server via POST. with some debugging i noticed that reading each part (of the multipart) takes the longest time
a
You can receive and read each part manually by chunks while observing a progress but in some cases you won’t know the size of a part before fully reading its body.
a
by manually, you mean to write my own ‘read part’ function? (dont have the code with me rn and cant remember the exact name)
a
I mean something like this:
Copy code
call.receiveMultipart().forEachPart { part ->
    when (part) {
        is PartData.FileItem -> {
            val input = part.provider()
            // Read from input by chunks
        }
        else -> {}
    }
}
👍 1
a
I think that the
forEachPart()
was taking most of the time and the saving the data to file was happening almost instantly. Ill report back whenever I get back to it
@Aleksei Tirman [JB] I revisited this and i can confirm the above.
Copy code
debugLn { ">>> 1. Receiving" }
            debugLn { ">>> 2. forEach" }
            call.receiveMultipart().forEachPart { partData ->
            debugLn { ">>> 3. looping" }
                when {
                    partData is PartData.FileItem -> {
                        uri = run {
                            debugLn { ">>> 4. running" }
                            val fileBytes = partData.streamProvider()
                            val fileName = partData.originalFileName.orEmpty()
                            debugLn { ">>> 5. saving" }
                            saveFileUseCase.saveFile(fileBytes, fileName).also {
                                debugLn { ">>> 6. saved" }
                            }
The above snippet prints 1 and 2 almost at the same time. Then 3 will be printed after the file is sent to the server. (I see the uploading progress on the client reaching 100% before 3. is hit). (debugln = println)
any way I can get the progress while the file is being uploaded to the ktor server?