https://kotlinlang.org logo
#ktor
Title
# ktor
m

Mantas Varnagiris

03/22/2020, 3:27 PM
Hi, I'm very new to web development so this is a very noob question. I'm trying to make a page where user can upload a file, then that file is processed and user gets output file for download. I can't find any info on how to achieve that (probably because I don't know what terms to use for search). Here's pseudo code of what I'm trying to do:
Copy code
fun Route.handleFile() {
    post("/handleFile") {
        call.respondTextWriter {
            appendln("Writing some output while file is being processed")
            // Handle file that will create another file for user to download
            call.respondFile(outputFile) // I know I cannot do this
        }
    }
}
Basically I want to run a long running task and show some text to the user about the progress and then after it finished that output file should be downloaded. Is that even possible to do just within ktor?
m

mp

03/22/2020, 4:08 PM
You might want to look into SSE or WebSockets to communicate progress.
m

Mantas Varnagiris

03/22/2020, 4:11 PM
Just to clarify. This is a server app. • User opens website on a browser • Uploads a file • While file is being processed user sees progress/log • When file finishes processing, result file should trigger to download automatically
m

mp

03/22/2020, 4:14 PM
Yes. SSE or WS are tools you could use to send progress updates to the browser.
m

Mantas Varnagiris

03/22/2020, 4:15 PM
OK I will look into these. Thanks. Can I use same thing for triggering file download for the user?
m

mp

03/22/2020, 4:23 PM
It’s up to the code running in the browser to decide what to do. Certainly possible you could send a “File’s done, here’s the ID/URL/whatever” event, and you could handle that by displaying a button to download the file.
m

Mantas Varnagiris

03/22/2020, 4:23 PM
Cool. Thanks very much for the pointers!
Another question: Is it possible to respond with file and HTML at the same time? Like
call.respondFileAndHtml(file) { HTML }
?
m

mp

03/22/2020, 4:49 PM
I doubt it. What Content-Type would you send such a response as?
m

Mantas Varnagiris

03/22/2020, 4:51 PM
well I;m just trying to see how to achieve: • Show some HTML or text • After a while send file
l

luke

03/24/2020, 2:25 AM
Could you show your HTML content and then perform a redirect? https://ktor.io/servers/calls/responses.html#redirections
2 Views