Kind: Interface
Source: packages/common/file-stream/interfaces/streamable-options.interface.ts
Part of: Common
Options for StreamableFile
StreamableFileOptions configures HTTP response metadata for a StreamableFile. It defines the file MIME type, optional content disposition header value(s), and content length so clients can correctly handle streamed file responses.
Properties
| Property | Type |
|---|---|
type | string |
disposition | `string |
length | number |
Diagram
mermaidgraph LR A[File stream or buffer] --> B[StreamableFile] C[StreamableFileOptions] --> B C --> D[type: Content-Type] C --> E[disposition: Content-Disposition] C --> F[length: Content-Length] B --> G[HTTP response]
Usage
tsimport { Controller, Get, StreamableFile } from '@nestjs/common';
import { createReadStream } from 'node:fs';
import { join } from 'node:path';
import type { StreamableFileOptions } from '@nestjs/common';
@Controller('reports')
export class ReportsController {
@Get('download')
download(): StreamableFile {
const filePath = join(process.cwd(), 'reports', 'monthly-report.pdf');
const options: StreamableFileOptions = {
type: 'application/pdf',
disposition: 'attachment; filename="monthly-report.pdf"',
length: 1024 * 256,
};
return new StreamableFile(createReadStream(filePath), options);
}
}
AI Coding Instructions
- Set
typeto a valid MIME type so browsers and API clients interpret the streamed content correctly. - Use
dispositionwithattachmentto force downloads, orinlinewhen the file should render in the browser. - Provide
lengthwhen known to enable an accurateContent-Lengthresponse header. - Ensure the declared
lengthmatches the actual stream or buffer size; incorrect values can cause incomplete downloads or hanging clients. - Pass these options directly to
StreamableFilewhen returning files from NestJS controllers.
Was this page helpful?