Ho un modello nell'API di loopback e voglio scaricarlo come file piuttosto che visualizzarlo come testo. Ho avuto qualche vecchio codice PHP che ho bastardizzato adattato per provare e scaricare la risposta come un file.Download di file dal loopback di Strongloop
Questo è il mio codice:
Issue.afterRemote('getCSV', function(ctx, affectedModelInstance, next) {
var result = ctx.result;
console.log(result);
var currentdate = new Date();
var datetime = currentdate.getDate() + " " +
+ (currentdate.getMonth()+1) + " " +
+ currentdate.getFullYear() + " " +
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds(); + " ";
ctx.res.set('Expires', 'Tue, 03 Jul 2001 06:00:00 GMT');
ctx.res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate');
ctx.res.set('Last-Modified', datetime +'GMT');
// force download
ctx.res.set('Content-Type','application/force-download');
ctx.res.set('Content-Type','application/octet-stream');
ctx.res.set('Content-Type','application/download');
// disposition/encoding on response body
ctx.res.set('Content-Disposition','attachment;filename=Data.csv');
ctx.res.set('Content-Transfer-Encoding','binary');
ctx.res.send(result);
}, function(err, response) {
if (err) console.error(err);
// next();
});
Ho visto le questioni sul download existing files with loopback, ma mai il download di una risposta REST come file.
Che aspetto ha il telecomando 'getCSV'? Perché non inserire questo codice in quel metodo remoto anziché come un hook? – jakerella