在GridFS,express,mongoDB,node.js中存储来自POST请求的数据流

 勇_哥6868 发布于 2023-02-07 12:30

我试图找出如何将图像直接发布到GridFS而不将其作为临时文件存储在服务器上的任何位置.

我使用Postman(chrome ext.)发布文件,我设法将此帖子存储为文件,使用:

req.pipe(fs.createWriteStream('./test.png'));

当从服务器上的文件创建readStream时,我还能够从readStream直接存储到GridFS.(见代码)

我有以下文件,saveFromReq.js它们监听POST,基本上只是将其传递给savePic.js.

saveFromReq.js:

var express = require('express');
var app = express();
var savePic = require('./savePic');
var fs = require('fs');
var GridStore = require('mongodb').GridStore;
var pic = './square.png';
var picID;



//When the following

 //var pic = fs.createReadStream('./square.png', {autoClose: true});

//is not commented out, and 'req' is replaced with 'pic' in the savePic function,
//the file square.png is stored correctly to GridFS

app.post('/picture', function(req, res){

    savePic(req, function(id){});
    res.writeHead(200, {'Content-Type': 'text' });
    res.end("Sucsess!\n");

});

app.listen(process.env.PORT || 3413);

savePic.js:

var savePic = function(req, callback){


var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');
    fs = require('fs');

    //When the following 

      //req.pipe(fs.createWriteStream('./test.png'));

    //is not commented out, the correct image is stored to test.png, and 
    //the sequence after req.on("data"... starts
    //(That sequence does not start at all when this is commented out..)

var fileId = new ObjectID();
var db = new Db('testDB', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {


  var gridStore = new GridStore(db, 'test', 'w');

  //open 
  gridStore.open(function(err, gridStore) {
    console.log("opened");


    req.on("data", function (data) {
        console.log("data recieved");
            gridStore.write(data, function (err, gridStore) {
                if (err) {
                    console.log("error writing file");
                }
            });
        });
     req.on("end", function () {
            gridStore.close(function (err, gridStore) {
                if (!err) {
                    console.log("The file has been stored to database.");
                    db.close();
                }
            });
        });
           req.pipe(gridStore);


      });




});
callback(fileId);
};
module.exports = savePic;

任何帮助将不胜感激!

1 个回答
  • gridfs-stream 使这很容易:

    // `gfs` is a gridfs-stream instance
    app.post('/picture', function(req, res) {
      req.pipe(gfs.createWriteStream({
        filename: 'test'
      }));
      res.send("Success!");
    });
    

    2023-02-07 12:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有