2016-02-13 27 views
1

我想寫一個從服務器返回到客戶端響應的緩衝區。鐵路由器動作使用Meteor.call拋出writeHead不是函數

所以我定義它運行到action功能獲取文件的路徑:

Router.route('/download/:blabla', { 
    name: 'download', 
    action: function() { 
     var that = this.response; 
     Meteor.call('downloadFile', blabla, function (err, result) { 
      // error check etc. 
      var headers = { 
       'Content-type' : 'application/octet-stream', 
       'Content-Disposition' : 'attachment; filename=' + fileName 
      }; 
      that.writeHead(200, headers); 
      that.end(result); 
     } 
    } 
}); 

此拋出:

Exception in delivering result of invoking 'downloadFile': TypeError: that.writeHead is not a function

沒有Metoer.call它的工作原理...

我正在使用nodejs流來獲取服務器端函數的緩衝區,它工作。

在此先感謝

+0

你爲什麼不嘗試設置'VAR是= this'和'that.response.writeHead(...)' –

+0

它也沒有幫助 –

回答

1

您是否嘗試過在服務器端使用IR路由器?即使用「{where:」服務器「}」在服務器上訪問路由,以避免必須按照下面的示例從here開始進行方法調用(注意在他的示例中需要添加記錄:npm包按照意見,但你也許能避免這個......):

Router.route("/file/:fileName", function() { 
    var fileName = this.params.fileName; 

    // Read from a file (requires 'meteor add meteorhacks:npm') 
    var filePath = "/path/to/pdf/store/" + fileName; 
    var fs = Meteor.npmRequire('fs'); 
    var data = fs.readFileSync(filePath); 

    this.response.writeHead(200, { 
    "Content-Type": "application/pdf", 
    "Content-Length": data.length 
    }); 
    this.response.write(data); 
    this.response.end(); 
}, { 
    where: "server" 
}); 
+0

也http://stackoverflow.com/questions/21565991/how-to-serve-a-file-using-iron-router-or-meteor-itself –