以下是我的工作流程:
在按钮点击事件中,我将搜索结果导出为 .csv 文件,并保存到服务器。一旦文件保存完毕,我希望将其发送到浏览器进行下载。使用这个问题《如何处理 Meteor.js 中的条件文件下载》,我创建了一个方法,在保存文件的方法返回后调用该方法。以下是该方法:
exportFiles: function(file_to_export) {
console.log("to export = "+file_to_export);
Meteor.Router.add('/export', 'GET', function() {
console.log('send '+file_to_export+' to browser');
return [200,
{
'Content-type': 'text/plain',
'Content-Disposition': "attachment; filename=" + this.request.query.file
}, fs.readFileSync( save_path + this.request.query.file )];
});
}
然而,我的问题是如何调用该路由?使用.Router.to('/export?file=filename.ext')
不起作用,并导致用户离开当前页面。我希望这对用户来说是无缝的,并且我不希望他们知道自己被重定向了。在任何人问之前,save_path
在方法外声明,因此它确实存在。