如何在Meteor中使用CollectionFS存储PDF文件?

时间:2016-03-27 23:30:06

标签: node.js mongodb meteor collectionfs

我正在开发 Meteor 应用程序。

目前,我的服务器上有一些PDF。要将这些已经存在的PDF直接提供给客户端,我这样做并且效果很好:

app.directive 'randomColor', () ->
  link: (scope) ->
    scope.colors = new Array
    col = 0x0
    while col <= 0xFFF
      if (col > 0x111 && col < 0xFFF)
        scope.colors.push '#' + col
      col++
    autocolor = (hexcode) ->
      colorChange = () ->
        $("#colorvomit").append("<span style='padding: 1px 10px 1px 10px;background-color: " +hexcode+";border: 1px solid black;'></span>")
      setTimeout(colorChange, 5000)
    _.each(scope.colors, autocolor)

我使用CollectionFS将这些PDF保存到Mongo(稍后,我将生成PDF并保存它们。现在,我只是将这些已经存在的PDF直接保存到Mongo,因为我首先要让Mongo部分工作。)。

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保存到Mongo后(如上所述),如何检索和提供这些PDF?

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

2 个答案:

答案 0 :(得分:1)

经过大量的搜索和尝试后,我终于开始工作了。

Router.route("/database/:pdfId", function() {
    var pdfId = this.params.pdfId;
    var file = testCollection.findOne({_id: pdfId});
    var readable = file.createReadStream("tmp");
    var buffer = new Buffer(0);
    readable.on("data", function(b) {
        buffer = Buffer.concat([buffer, b]);
    });
    var response = this.response;
    readable.on("end", function() {
        response.writeHead(200, {
            "Content-Type": "application/pdf",
            "Content-Length": buffer.length
        });
        response.write(buffer);
        response.end();
    });
}, {
    where: "server"
});

答案 1 :(得分:0)

我知道这个问题已经过时了,但我找到了一种更简单的方法来存储和检索PDF。显然,如果您将PDF存储在数据库中并且它们小于16MB(这可能在此类文件中),则性能会比将文件存储在服务器文件系统中的速度慢。

为此,您可以使用FS.Store.FileSystem而不是FS.Store.GridFS。以下代码适用于我:

// Client
var pdfStore = new FS.Store.FileSystem("uploadedFiles");
UploadedFiles = new FS.Collection("uploadedFiles", {
  stores: [pdfStore],
  filter: {
    allow: {
      extensions: ['pdf','doc','docx','xls','xlsx','ppt','pptx''jpg','png','jpeg']
    }
  }
});

// Server
var pdfStore = new FS.Store.FileSystem("uploadedFiles", {path: uploadFilesPath});
UploadedFiles = new FS.Collection("uploadedFiles", {
  stores: [pdfStore],
  filter: {
    maxSize: 5242880, // 5MB in bytes
    allow: {
      extensions: ['pdf','doc','docx','xls','xlsx','ppt','pptx''jpg','png','jpeg']
    },
    deny: {
      extensions: ['exe']
    },
    onInvalid: function (message) {
      if (Meteor.isClient) {
        alert(message);
      } else {
        console.log(message);
      }
    }
  }
});

然后只需使用这个小助手来检索文件的URL:

  get_uploaded_link: function(id){
    console.log(id);
    var file = UploadedFiles.findOne({_id: id});
    return file.url();}