Skip to content

Commit 28488a9

Browse files
Merge pull request #2010 from eamonnmcevoy/2009-add-response-to-streamable-file-example
docs(streaming-files): show how to set content-type and content-dispo…
2 parents 1be4249 + 0af91cf commit 28488a9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

content/techniques/streaming-files.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,25 @@ export class FileController {
4545
}
4646
}
4747
```
48+
49+
The default content type is `application/octet-stream`, if you need to customize the response you can use the `res.set` method.
50+
51+
52+
```ts
53+
import { Controller, Get, StreamableFile, Response } from '@nestjs/common';
54+
import { createReadStream } from 'fs';
55+
import { join } from 'path';
56+
57+
@Controller('file')
58+
export class FileController {
59+
@Get()
60+
getFile(@Response({ passthrough: true }) res): StreamableFile {
61+
const file = createReadStream(join(process.cwd(), 'package.json'));
62+
res.set({
63+
'Content-Type': 'application/json',
64+
'Content-Disposition': 'attachment; filename="package.json"',
65+
});
66+
return new StreamableFile(file);
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)