【问题标题】:store images in mongodb using mongoose?? how to?使用猫鼬将图像存储在mongodb中?如何?
【发布时间】:2012-08-16 07:36:33
【问题描述】:

任何人都有一个使用 mongoose 和 nodejs express 在 mongodb 中插入图像的快速示例

我读了一些例子,但我不明白怎么做??

我想上传一张表格中的图片

app.post('/videos/new', function(req, res) {
  req.form.complete(function(err, fields, files) {
    console.log('here i go');
    if(err) {
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      console.log('insssssssssssss'+ins);
      ous = fs.createWriteStream(__dirname + '/static/uploads/videos/' + files.file.filename);
      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else { RegProvider.save({
           file: req.param(files.file.filename),
                   filename: req.param('filename')
                 }, function(error, docs) {
              res.redirect('/videos');
         });
        }
      });
      //console.log('\nUploaded %s to %s', files.file.filename, files.file.path);
      //res.send('Uploaded ' + files.file.filename + ' to ' + files.file.path);
    }
  });
});

喜欢这个例子,但我不明白它是如何工作的

【问题讨论】:

  • 您最好将文件上传到 S3 之类的东西,然后将该文件的公共 URL 存储在数据库中。海量图像数据在几乎任何数据库上都会产生一些严重的性能问题。
  • 处理这个用例的标准 MongoDB 方法是使用 GridFS:mongodb.org/display/DOCS/GridFS
  • 这不是用于大容量存储...而是用于建议的 tnx
  • JohnnyHK你能给我举个例子来上传一个jpg文件吗??
  • StackOverflow 上已多次提出类似问题,因此您可以查看过去的答案。如果您的图像不太可能超过当前 16Mb BSON 文档大小限制,您可以将图像序列化为二进制并保存在文档中,而不是使用 GridFS。这是一个示例要点:store/display an image in mongodb using mongoose/express.

标签: node.js mongodb express mongoose


【解决方案1】:

util.pumpdeprecated

您可以尝试https://github.com/aheckmann/gridformhttps://github.com/aheckmann/connect-multipart-gridform 看看它们是否满足您的需求。

【讨论】:

    猜你喜欢
    • 2015-02-28
    • 2017-08-20
    • 2011-05-29
    • 2015-02-05
    • 1970-01-01
    • 2018-03-11
    • 2019-05-06
    • 2016-04-08
    • 1970-01-01
    相关资源
    最近更新 更多