Meteor 下载文件的服务器端路由

Meteor 下载文件的服务器端路由,meteor,iron-router,Meteor,Iron Router,我有一个下载文件的服务器端路径。这是从客户端按钮点击调用的,一切正常。但是,一旦按钮被单击一次,它将不会再次工作,直到加载另一个路由并返回。如何对其进行编码,以便可以多次单击该按钮并每次触发服务器端路由 我的按钮代码如下所示 'click #view_document_download': function (event, tmpl) { Router.go('/download_document/' + this._id); } Router.route('/download_docu

我有一个下载文件的服务器端路径。这是从客户端按钮点击调用的,一切正常。但是,一旦按钮被单击一次,它将不会再次工作,直到加载另一个路由并返回。如何对其进行编码,以便可以多次单击该按钮并每次触发服务器端路由

我的按钮代码如下所示

'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' });
我的服务器端路由如下所示

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

也许您应该通过一个方法从服务器返回一个对象,并在客户端将其形成一个文件?如果可能的话

在客户端创建文件非常简单,此时不必处理路由器

函数输出文件(文件名、数据){
var blob=new blob([data],{type:'text/plain'});//!注意文件类型。。
if(window.navigator.msSaveOrOpenBlob){
window.navigator.msSaveBlob(blob,文件名);
}
否则{
var elem=window.document.createElement('a');
elem.href=window.URL.createObjectURL(blob);
elem.download=文件名;
document.body.appendChild(elem)
元素单击();
文件.body.removeChild(elem);
}
}
函数getContentAndOutputFile(){
var content=document.getElementById('content').value;
outputFile('file.txt',content);
}


创建文件
使用
a
元素而不是js'click'事件
页面html

<a href="/download_document/{{_id}}" download="true" target="_blank"></a> 

我可以这样做,但我有一个工作的解决方案,这个服务器端,它只是路线,我想重置每一次。我更想知道这是否可行。此外,我还想了解您的解决方案对标准服务器端下载的性能影响?好吧,关于性能,很简单,您只需将文件下载一次到客户端。下一次单击时,它只会获取相同的文件,而不会对服务器造成压力