: Sends the file in small "chunks". This is the professional standard as it keeps memory usage low regardless of file size. Method 1: Using the StreamableFile Class (Recommended)
: By default, this returns the file with a Content-Type of application/octet-stream . Method 2: Custom Headers for Browser Behavior download files nestjs
: Loads the entire file into the server's memory before sending it. This is fine for small files but can crash your server if many users try to download large files simultaneously. : Sends the file in small "chunks"
To download a file in NestJS , you generally choose between two methods based on file size and performance needs: Method 2: Custom Headers for Browser Behavior :
Managing file downloads in is a fundamental task for building robust APIs that serve documents, images, or large data exports like CSVs.
To force the browser to "save" the file with a specific name rather than opening it in a tab, you must set the Content-Disposition header. Example: Setting a Filename Streaming Files | NestJS - A progressive Node.js framework
While standard JSON responses are the norm, downloading files requires handling binary data, streams, and specific HTTP headers. Core Concepts: Buffer vs. Stream