我试图找出如何将图像直接发布到GridFS而不将其作为临时文件存储在服务器上的任何位置.
我使用Postman(chrome ext.)发布文件,我设法将此帖子存储为文件,使用:
req.pipe(fs.createWriteStream('./test.png'));
Run Code Online (Sandbox Code Playgroud)
当从服务器上的文件创建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 …Run Code Online (Sandbox Code Playgroud)