【发布时间】:2017-06-05 20:45:43
【问题描述】:
我有一个用于下载文件的服务器端路由。这是从客户端按钮单击调用的,一切正常。但是,一旦该按钮被单击一次,它将无法再次工作,直到加载另一条路线并返回。如何编码,以便可以多次单击按钮并每次触发服务器端路由?
我的按钮代码如下所示...
'click #view_document_download': function (event, tmpl) {
Router.go('/download_document/' + this._id);
}
我的服务器端路由看起来像这样......
Router.route('/download_document/:_id', function () {
//Get the file record to download
var file = files.findOne({_id: this.params._id});
//Function to take a cfs file and return a base64 string
var getBase64Data = function(file2, callback) {
var readStream = file2.createReadStream();
var buffer = [];
readStream.on('data', function(chunk) {
buffer.push(chunk);
});
readStream.on('error', function(err) {
callback(err, null);
});
readStream.on('end', function() {
callback(null, buffer.concat()[0].toString('base64'));
});
};
//Wrap it to make it sync
var getBase64DataSync = Meteor.wrapAsync(getBase64Data);
//Get the base64 string
var base64str = getBase64DataSync(file);
//Get the buffer from the string
var buffer = new Buffer(base64str, 'base64');
//Create the headers
var headers = {
'Content-type': file.original.type,
'Content-Disposition': 'attachment; filename=' + file.original.name
};
this.response.writeHead(200, headers);
this.response.end(buffer, 'binary');
}, { where: 'server' });
【问题讨论】:
标签: meteor iron-router