从Strongloop环回下载文件

我在回送API中有一个模型,我想下载它作为一个文件,而不是显示为文本。 我有一些旧的PHP代码,我已经适应尝试和下载响应作为一个文件。

这是我的代码:

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(); }); 

我已经看到有关使用环回下载现有文件的问题,但是从不下载REST响应作为文件。

根据你的方法,它是这样工作的。 就我而言,“组织”就是模型。

文件:common / models / 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. }; 

而远程方法定义(获取响应对象)

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

虽然types只是一个获取不同的CSV文件导出参数..

注意:我正在使用“loopback”:“^ 2.10.2”,