In this post, we will learn how to perform NestJS File Download Stream with examples. There are many times we would like to download files from the server application.

In the previous post, we had learned how to upload a file using NestJS. You can go through that post to get a better idea of how file handling works in NestJS.

1 – The StreamableFile Class

Technically, we can simply download a file using the standard response object. However, it is better to the use the StreamableFile class to perform the job in NestJS.

The StreamableFile is a class that holds on to the stream that is to be returned. To create an instance of StreamableFile, you can pass it a buffer or a stream. The class itself is imported from the @nestjs/common package.

Also, StreamableFile works with both Express and Fastify. Therefore, we don’t need to worry about compatibility between the two engines when switching from one to the other.

2 – NestJS File Download Stream Basic Example

Let us look at a basic example first.

@Get('stream-file')
getFile(): StreamableFile {
  const file = createReadStream(join(process.cwd(), 'package.json'));
  return new StreamableFile(file)
}

Below are the necessary imports.

import { createReadStream } from 'fs';
import { join } from 'path';

Here, we are simply returning the package.json file from a request handler. We use the createReadStream() to obtain a stream and pass it to the StreamableFile constructor.

If you wish to read more about controllers, refer to this detailed post on creating your first NestJS Controller.

The default content-type is application/octet-stream. Basically, using the endpoint, we can download the package.json file. However, the name of the file will be stream-file.

3 – NestJS Download Stream Customized Response

We can also customize the response. Basically, we have to use the res.set() method for the same. See below example:

@Get('stream-file-customize')
getFileCustomizedResponse(@Response({ passthrough: true }) res): StreamableFile {
    const file = createReadStream(join(process.cwd(), 'package.json'));
    res.set({
      'Content-Type': 'application/json',
      'Content-Disposition': 'attachment; filename="package.json'
    })
    return new StreamableFile(file);
}

Here, we specify the content-type as application/json. Also, we set the content-disposition where we specify the filename.

This time, the name of the file will be package.json and it will be also have content-type as json.

Conclusion

With this, we have successfully learnt how to perform NestJS File Download Stream and also how to customize the same using the Response object.

The code for this post is available on Github.

If you have any comments or queries, please mention in the comments section below.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *