2015-03-31 11 views
9

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.

+1

Che aspetto ha il telecomando 'getCSV'? Perché non inserire questo codice in quel metodo remoto anziché come un hook? – jakerella

risposta

19

in base al vostro approccio funziona così. Nel mio caso "l'organizzazione" è il modello.

File: comuni/modelli/organisation.js

Organisation.csvexport = function(type, res, callback) { 
    //@todo: get your data from database etc... 
    var datetime = new Date(); 
    res.set('Expires', 'Tue, 03 Jul 2001 06:00:00 GMT'); 
    res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate'); 
    res.set('Last-Modified', datetime +'GMT'); 
    res.set('Content-Type','application/force-download'); 
    res.set('Content-Type','application/octet-stream'); 
    res.set('Content-Type','application/download'); 
    res.set('Content-Disposition','attachment;filename=Data.csv'); 
    res.set('Content-Transfer-Encoding','binary'); 
    res.send('ok;'); //@todo: insert your CSV data here. 
}; 

E la definizione del metodo remoto (per ottenere l'oggetto Response)

Organisation.remoteMethod('csvexport', 
{ 
    accepts: [ 
    {arg: 'type', type: 'string', required: true }, 
    {arg: 'res', type: 'object', 'http': {source: 'res'}} 
    ], 
    returns: {}, 
    http: {path: '/csvexport/:type', verb: 'get'} 
}); 

Mentre tipo è solo un ottenere parametri per le diverse Esportazione file CSV ..

Nota: sto utilizzando "loopback": "^ 2.10.2",

+0

Grazie. Risposta fantastica Definire le intestazioni di risposta è stato il trucco! – Richard

Problemi correlati