Instance Method
streamFile(at:chunkSize:mediaType:onCompleted:)
Generates a chunked
Response for the specified file. This method respects values in the "ETag" header and is capable of responding 304 Not Modified if the file in question has not been modified since last served. This method will also set the "Content-Type" header automatically if an appropriate MediaType can be found for the file’s suffix.@preconcurrency func streamFile(at path: String, chunkSize: Int = NonBlockingFileIO.defaultChunkSize, mediaType: HTTPMediaType? = nil, onCompleted: @escaping @Sendable (Result<Void, any Error>) -> () = { _ in }) -> Response
Parameters
pathPath to file on the disk.
chunkSizeMaximum size for the file data chunks.
mediaTypeHTTPMediaType, if not specified, will be created from file extension.
onCompletedClosure to be run on completion of stream.
Return Value
A 200 OK response containing the file stream and appropriate headers.
Discussion
app.get("file-stream") { req in
return req.fileio.streamFile(at: "/path/to/file.txt")
}