Skip to content

Commit 2bcdb0a

Browse files
Merge pull request #2430 from micalevisk/patch-2
docs(techniques): enhance `StreamableFile` code snippet
2 parents 90e2985 + a42c4e2 commit 2bcdb0a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

content/techniques/streaming-files.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,33 @@ export class FileController {
4646
}
4747
```
4848

49-
The default content type is `application/octet-stream`, if you need to customize the response you can use the `res.set` method.
49+
The default content type is `application/octet-stream`, if you need to customize the response you can use the `res.set` method or the [`@Header()`](/controllers#headers) decorator, like this:
5050

5151
```ts
52-
import { Controller, Get, StreamableFile, Response } from '@nestjs/common';
52+
import { Controller, Get, StreamableFile, Res } from '@nestjs/common';
5353
import { createReadStream } from 'fs';
5454
import { join } from 'path';
55+
import type { Response } from 'express';
5556

5657
@Controller('file')
5758
export class FileController {
5859
@Get()
59-
getFile(@Response({ passthrough: true }) res): StreamableFile {
60+
getFile(@Res({ passthrough: true }) res: Response): StreamableFile {
6061
const file = createReadStream(join(process.cwd(), 'package.json'));
6162
res.set({
6263
'Content-Type': 'application/json',
6364
'Content-Disposition': 'attachment; filename="package.json"',
6465
});
6566
return new StreamableFile(file);
6667
}
68+
69+
// Or even:
70+
@Get()
71+
@Header('Content-Type', 'application/json')
72+
@Header('Content-Disposition', 'attachment; filename="package.json"')
73+
getStaticFile(): StreamableFile {
74+
const file = createReadStream(join(process.cwd(), 'package.json'));
75+
return new StreamableFile(file);
76+
}
6777
}
6878
```

0 commit comments

Comments
 (0)