0
我正在做一個流星申請。如何提供使用CollectionFS在Meteor中存儲的PDF文件?
目前,我的服務器上有幾個PDF文件。要直接服務於這些已有的PDF文件到客戶端,我做這種方式,它工作得很好。
Router.route("/file/:fileName", function() {
var fileName = this.params.fileName;
// console.log(process.env.PWD);
var filePath = process.env.PWD + '/' + 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"
});
我節省使用CollectionFS(後來,我將生成PDF文件,並將它們保存這些PDF到蒙戈現在,我只是直接將這些已經存在的PDF保存到Mongo中,因爲我首先想讓Mongo部分工作。)。
testCollection = new FS.Collection("testCollection", {
stores: [new FS.Store.GridFS("testCollection")]
});
testCollection.allow({
'insert': function() {
return true;
}
});
var file = new FS.File(process.env.PWD + '/PDFKitExampleServerSide.pdf');
file.encoding = 'binary';
file.name('myPDF.pdf');
var document = testCollection.insert(file);
console.log(document._id);
我的問題是,當我使用保存這些CollectionFS PDF文件到蒙戈(就像我上面做的),我該如何檢索和服務於這些PDF文件?
Router.route("/database/:pdfId", function() {
//need help here
}, { where: "server"});