Skip to content

Commit 3b129fc

Browse files
Merge pull request #3052 from micalevisk/patch-1
docs(techniques): add mention about available options of `StreamableFile`
2 parents 6500880 + 50bf378 commit 3b129fc

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

content/techniques/streaming-files.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,30 @@ 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 or the [`@Header()`](/controllers#headers) decorator, like this:
49+
The default content type (the value for `Content-Type` HTTP response header) is `application/octet-stream`. If you need to customize this value you can use the `type` option from `StreamableFile`, or use the `res.set` method or the [`@Header()`](/controllers#headers) decorator, like this:
5050

5151
```ts
5252
import { Controller, Get, StreamableFile, Res } from '@nestjs/common';
5353
import { createReadStream } from 'fs';
5454
import { join } from 'path';
55-
import type { Response } from 'express';
55+
import type { Response } from 'express'; // Assuming that we are using the ExpressJS HTTP Adapter
5656

5757
@Controller('file')
5858
export class FileController {
5959
@Get()
60-
getFile(@Res({ passthrough: true }) res: Response): StreamableFile {
60+
getFile(): StreamableFile {
61+
const file = createReadStream(join(process.cwd(), 'package.json'));
62+
return new StreamableFile(file, {
63+
type: 'application/json',
64+
disposition: 'attachment; filename="package.json"',
65+
// If you want to define the Content-Length value to another value instead of file's length:
66+
// length: 123,
67+
});
68+
}
69+
70+
// Or even:
71+
@Get()
72+
getFileChangingResponseObjDirectly(@Res({ passthrough: true }) res: Response): StreamableFile {
6173
const file = createReadStream(join(process.cwd(), 'package.json'));
6274
res.set({
6375
'Content-Type': 'application/json',
@@ -70,7 +82,7 @@ export class FileController {
7082
@Get()
7183
@Header('Content-Type', 'application/json')
7284
@Header('Content-Disposition', 'attachment; filename="package.json"')
73-
getStaticFile(): StreamableFile {
85+
getFileUsingStaticValues(): StreamableFile {
7486
const file = createReadStream(join(process.cwd(), 'package.json'));
7587
return new StreamableFile(file);
7688
}

0 commit comments

Comments
 (0)