【问题标题】:How to serve a PDF file stored using CollectionFS in Meteor?如何在 Meteor 中提供使用 CollectionFS 存储的 PDF 文件?
【发布时间】:2016-07-15 05:07:12
【问题描述】:

我正在开发一个 Meteor 应用程序。

目前,我的服务器上有一些 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 保存到 Mongo(稍后,我将生成 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 保存到 Mongo 之后(就像我在上面所做的那样),我如何检索和提供这些 PDF?

Router.route("/database/:pdfId", function() {
//need help here
}, { where: "server"});

【问题讨论】:

    标签: node.js mongodb meteor collectionfs


    【解决方案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"
    });
    

    【讨论】:

      【解决方案2】:

      我知道这个问题很老,但我找到了一种更简单的方法来存储和检索 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();}
      

      【讨论】:

        猜你喜欢
        • 2018-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多