diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index d3ac682..cc32386 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -57,3 +57,19 @@ def send_file(env, path : String, mime_type : String? = nil) IO.copy(file, env.response) end end + +# Send a file with given data and default `application/octet-stream` mime_type. +# +# send_file env, data_slice +# +# Optionally you can override the mime_type +# +# send_file env, data_slice, "image/jpeg" + +def send_file(env, data : Slice(UInt8), mime_type : String? = nil) + mime_type ||= "application/octet-stream" + + env.response.content_type = mime_type + env.response.content_length = data.bytesize + env.response.write data +end