-3

I need to make an APIto download a static file test.js.

When I test the API with Postman the result is just display the content of file, not downloading.

Is that ok?

async downloadTemplate(req , res) {
    let fs = require('fs');
    if(fs.existsSync('downloads/test.js'))
    {   
        res.setHeader('Content-disposition', 'attachment; filename=' + 'test.js');

        let filestream = fs.createReadStream('downloads/test.js');
        //return res.download('downloads/test.js' , 'test.js')
        return filestream.pipe(res);
    } else {
        res.json({error : "File not Found"});
    }
}
jrswgtr
  • 2,287
  • 8
  • 23
  • 49

1 Answers1

0

Postman should not be used for downloading files.

The client that will consume the API should decide what to do with the result.

If the client is going to be a browser - just test the api with a browser, not with postman.

In case the link to the server is in html - use the download attribute in the href

In case you are using Axios you can find the help you need here

yeya
  • 1,968
  • 1
  • 21
  • 31