Closed STRML closed 7 years ago
@STRML @superkhau
Campaign.afterRemote("**",function campaignAfterRemoteDonations(ctx,error,next){
console.log('campaignAfterRemoteDonations',ctx.method.name);
if( ctx.req.query.format != undefined )
if( ctx.req.query.format.toLowerCase() == 'csv' ){
ctx.res.setHeader("Content-Type", "text/csv");
ctx.res.setHeader('Content-Type','application/force-download');
ctx.res.setHeader('Content-Type','application/octet-stream');
ctx.res.setHeader('Content-Type','application/download');
ctx.res.setHeader('Content-Transfer-Encoding','binary');
ctx.res.setHeader('Content-Disposition', 'attachment; filename=example.csv');
ctx.res.end(ctx.result);
console.log( ctx.result );
}
next( );
});
@STRML are you okay it the solution above? Been over the year since this issue was opened.
This might take a while for us to get to if you still require this. To speed up the process, you can do one of two things:
I am closing; please re-open if solution above does not work for you.
Yeah, we did something similar in the end:
// Add CSV support.
// Yeah, really. Misspelled.
const resolveReponseOperation = HttpContext.prototype.resolveReponseOperation;
const csvReponse = { // trololol
contentType: 'text/csv',
sendBody: sendCSVBody
};
HttpContext.prototype.resolveReponseOperation = function(accepts) {
if (accepts === 'text/csv' || accepts === 'csv') return csvReponse;
return resolveReponseOperation.call(this, accepts);
};
function sendCSVBody(res: Response, data: Array<Object> | Object): void {
if (!data.length || typeof data[0] !== 'object') {
res.statusCode === 204;
} else {
res.send(json2csv({data}));
}
}
This supports query strings like _format=csv
as well.
Is there any way to do this (say, inject a post-request, pre-response middleware)? Would really like to support
GET /model(.json)
,GET /model.xml
, andGET /model.csv
, as well asGET /model
with an Accepts header.