9
我有一個環回API的模型,我想下載它作爲一個文件,而不是顯示爲文本。我有一些舊的PHP代碼,我有混蛋適應嘗試和下載響應作爲一個文件。從Strongloop環回下載文件
這是我的代碼:
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響應爲文件的問題。
你的'getCSV'遙控器是什麼樣的?爲什麼不把這個代碼放在遠程方法而不是鉤子? – jakerella 2015-03-31 17:36:22