I have a controller method in my inversify-express-utils app that looks something like this:
return await this.downloadReportUseCase.invoke(String(req.query.report_id), String(req.query.user_id))
.then(async ([filename, filedata]: [string, string]) => {
return res.status(200).sendFile(fs.writeFileSync(filename, filedata)); // I want to do something like this
})
.catch((err: Error) => {
res.status(500).json(err);
});
Note the comment, how do i achieve something like that? I don't actually want to create a file on the server, i just want a the variable to be sent to the end user in the form of a file that is either XML or CSV file type.
CodePudding user response:
With fs.writeFileSync
you do actually create a file on the server. Instead, use
res.set("Content-Disposition", "attachment; filename=" filename);
res.end(filedata);
CodePudding user response:
A combination of this question: How to download files using axios
and using res.attachment
in my controller allowed me to download the file