Skip to content


Vapor offers a simple API for reading and writing files asynchronously within route handlers. This API is built on top of NIO's NonBlockingFileIO type.


The main method for reading a file delivers chunks to a callback handler as they are read off the disk. The file to read is specified by its path. Relative paths will look in the process's current working directory.

// Asynchronously reads a file from disk.
req.fileio.readFile(at: "/path/to/file") { chunk in
    print(chunk) // ByteBuffer

The returned future will signal when the read has completed or an error has occured.


The streamFile method converts a streaming file to a Response. This method will set appropriate headers such as ETag and Content-Type automatically.

// Asynchronously streams file as HTTP response.
req.fileio.streamFile(at: "/path/to/file").map { res in
    print(res) // Response

The result can be returned directly by your route handler.


The collectFile method reads the specified file into a buffer.

// Reads the file into a buffer.
req.fileio.collectFile(at: "/path/to/file").map { buffer in 
    print(buffer) // ByteBuffer


This method requires the entire file to be in memory at once. Use chunked or streaming read to limit memory usage.


The writeFile method supports writing a buffer to a file.

// Writes buffer to file.
req.fileio.writeFile(ByteBuffer(string: "Hello, world"), at: "/path/to/file")

The returned future will signal when the write has completed or an error has occured.


For more information on serving files from your project's Public folder automatically, see Middleware → FileMiddleware.


For cases that Vapor's API doesn't support, you can use NIO's NonBlockingFileIO type directly.

// Main thread.
let fileHandle = try app.fileio.openFile(
    path: "/path/to/file", 

// In a route handler.
    path: "/path/to/file", 
    eventLoop: req.eventLoop
).map { fileHandle in

For more information, visit SwiftNIO's API reference.