Skip to content

Commit e4b25d6

Browse files
author
Eamonn McEvoy
committed
docs(streaming-files): split StreamableFile example into 2 parts.
1 parent 56ced55 commit e4b25d6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

content/techniques/streaming-files.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,35 @@ Fastify, by default, can support sending files without needing to call `stream.p
3131

3232
You can find a simple example of returning the `package.json` as a file instead of a JSON below, but the idea extends out naturally to images, documents, and any other file type.
3333

34-
> info **hint** If the @Response parameter is present, it must have `{{ '{' }} passthrough: true {{ '}' }}`.
34+
```ts
35+
import { Controller, Get, StreamableFile } from '@nestjs/common';
36+
import { createReadStream } from 'fs';
37+
import { join } from 'path';
38+
39+
@Controller('file')
40+
export class FileController {
41+
@Get()
42+
getFile(): StreamableFile {
43+
const file = createReadStream(join(process.cwd(), 'package.json'));
44+
return new StreamableFile(file);
45+
}
46+
}
47+
```
48+
49+
The default content type is `application/octet-stream`, if you need to customize the response you can use `res.set`.
50+
51+
> info **hint** If the @Response parameter is present, it must have `{ passthrough: true }`.
3552
3653
```ts
37-
import { Controller, Get, StreamableFile, Response } from '@nestjs/common';
54+
import { Controller, Get, StreamableFile } from '@nestjs/common';
3855
import { createReadStream } from 'fs';
3956
import { join } from 'path';
4057

4158
@Controller('file')
4259
export class FileController {
4360
@Get()
44-
getFile(@Response({ passthrough: true }) res): StreamableFile {
61+
@Response({ passthrough: true }) res,
62+
getFile(): StreamableFile {
4563
const file = createReadStream(join(process.cwd(), 'package.json'));
4664
res.set({
4765
'Content-Type': 'application/json',
@@ -50,4 +68,4 @@ export class FileController {
5068
return new StreamableFile(file);
5169
}
5270
}
53-
```
71+
```

0 commit comments

Comments
 (0)