Added a new method for file download and correct csv is getting added.

This commit is contained in:
Owaise Imdad
2025-06-19 22:23:10 +05:30
parent e6b9b5df3c
commit a1c11fbb3c
2 changed files with 16 additions and 2 deletions

View File

@@ -727,8 +727,7 @@ class MonitorController {
const csv = pkg.unparse(csvData);
return res.success({
msg: this.stringService.monitorsExported,
return res.file({
data: csv,
headers: {
"Content-Type": "text/csv",

View File

@@ -45,6 +45,21 @@ const responseHandler = (req, res, next) => {
data,
});
};
/**
* Sends a raw file response (for CSV, PDF, etc.)
* @param {Object} options
* @param {Buffer|string} options.data - The file content
* @param {Object} options.headers - Headers to set (e.g. Content-Type, Content-Disposition)
* @param {number} [options.status=200] - HTTP status code
*/
res.file = ({ data, headers = {}, status = 200 }) => {
Object.entries(headers).forEach(([key, value]) => {
res.setHeader(key, value);
});
return res.status(status).send(data);
};
next();
};