从 Strongloop 环回下载文件
Downloading file from Strongloop loopback
我在环回 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();
});
我见过有关下载 existing files with loopback 的问题,但从未将 REST 响应下载为文件。
根据您的方法,它是这样工作的。在我的例子中 'organisation' 是模型。
文件: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'}
});
虽然类型只是不同 CSV 文件导出的获取参数..
注:我用的是"loopback": "^2.10.2",
我在环回 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();
});
我见过有关下载 existing files with loopback 的问题,但从未将 REST 响应下载为文件。
根据您的方法,它是这样工作的。在我的例子中 'organisation' 是模型。
文件: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'}
});
虽然类型只是不同 CSV 文件导出的获取参数..
注:我用的是"loopback": "^2.10.2",