Ktor: Download file with custom name

Created on 15 Aug 2018  路  2Comments  路  Source: ktorio/ktor

Hello,

I'm trying to download an auto generated json content. The only way it works is with respondBytes, but you can't specify the name of the file, it's always the route name. I wan't to specify the name depending the user and the extension.

This is what I'm using, where json is the content to download.

call.respondBytes(json, ContentType.Application.OctetStream)

Thanks.

documentation

Most helpful comment

I guess you have to use the Content-Disposition header:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

// Untyped:
call.response.header(HttpHeaders.ContentDisposition, "attachment; filename=\"myfilename.bin\"")

// Typed:
call.response.header(HttpHeaders.ContentDisposition, ContentDisposition.Attachment.withParameter(ContentDisposition.Parameters.FileName, "myfile.bin").toString())

I have updated the documentation to reflect this: http://ktor.io/servers/calls/responses.html#content-disposition

All 2 comments

I guess you have to use the Content-Disposition header:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

// Untyped:
call.response.header(HttpHeaders.ContentDisposition, "attachment; filename=\"myfilename.bin\"")

// Typed:
call.response.header(HttpHeaders.ContentDisposition, ContentDisposition.Attachment.withParameter(ContentDisposition.Parameters.FileName, "myfile.bin").toString())

I have updated the documentation to reflect this: http://ktor.io/servers/calls/responses.html#content-disposition

Thanks. Resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

evgfilim1 picture evgfilim1  路  4Comments

chrisjenx picture chrisjenx  路  4Comments

lamba92 picture lamba92  路  3Comments

dedward3 picture dedward3  路  4Comments

ManifoldFR picture ManifoldFR  路  4Comments