我使用nodeJS和mongoDB創建數據輸入表單。部分表格要求用戶上傳圖片。我發現了一個資源,可以讓你做到這一點,但現在這個代碼已被棄用。使用GridFS在MongoDB 2015中保存和加載圖像
app.use(express.bodyParser((uploadDir: __dirname + '/public/uploads')));
這裏 Link
建議此文件保存到上傳目錄,但顯然不是MongoDB中我一直在使用busboy
嘗試。
的Youtube視頻Link
的YouTube視頻的結果正是我需要爲我的網站。我只需要知道將使這種可能的代碼,現在身體分析器被快速棄用。
這裏是我的全部的NodeJS代碼:
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var fs = require('fs');
var GridStore = mongo.GridStore;
var Server = mongo.Server;
var ObjectID = mongo.ObjectId;
var Db = mongo.Db;
var busboy = require('connect-busboy');
var server = new Server('localhost',27017,{auto_reconnect:true});
var db = new Db('exampleDb',server);
db.open(function(err,db){
if(err)
{
console.log('Couldnt connect to mongo');
}else{
console.log('Connected to Mongo');
}
});
//app.use(express.static(__dirname +'/public'));
app.use(express.static(path.join(__dirname, 'public')));
//app.use(express.bodyParser((uploadDir: __dirname + '/public/uploads')));
// app.use(bodyParser({uploadDir: __dirname + '/public/uploads'}));
app.use(busboy());
//app.use(bodyParser.urlencoded({
// extended: true
//}));
app.get('/index.html', function (req, res) {
res.sendFile(__dirname + "index.html");
});
app.post('/upload',function(req,res){
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + filename);
fstream = fs.createWriteStream(__dirname + '/assets/uploads/' + filename);
file.pipe(fstream);
fstream.on('close', function() {
res.redirect('back');
});
});
console.dir(req.file);
});
app.listen(3000);
更新:
我設法得到它的工作上傳,但觀看的圖片我仍然有以下問題:
MongoError: file with id 55d5a3416aebd290131ca6d2 not opened for writing c:\MyProjects\gridFStesting\node_modules\mongodb\lib\utils.js:97 process.nextTick(function() { throw err; }); ^ TypeError: Cannot read property 'stream' of undefined
這是我的更新的代碼:
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var fs = require('fs');
var GridStore = mongo.GridStore;
var Server = mongo.Server;
var ObjectID = mongo.ObjectId;
var Db = mongo.Db;
var busboy = require('connect-busboy');
var Grid = require('gridfs-stream');
var db = new mongo.Db('testing',new mongo.Server("127.0.0.1",27017));
var gfs = Grid(db,mongo);
db.open(function(err,db){
if(err)
{
console.log('Couldnt connect to mongo');
}else{
console.log('Connected to Mongo');
}
});
//app.use(express.static(__dirname +'/public'));
app.use(express.static(path.join(__dirname,'public')));
//app.use(express.bodyParser((uploadDir: __dirname + '/public/uploads')));
// app.use(bodyParser({uploadDir: __dirname + '/public/uploads'}));
app.use(busboy());
//app.use(bodyParser.urlencoded({
// extended: true
//}));
app.get('/index.html', function (req, res) {
res.sendFile(__dirname + "index.html");
});
app.post('/upload',function(req,res){
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname + ']: filename: ' + filename);
var gfsstream = gfs.createWriteStream('pic');
file.pipe(gfsstream).on('finish', function() {
console.log('File [' + fieldname + '] Finished');
console.log(gfsstream);
});
});
// var gridStoreWrite = new GridStore(db,new ObjectID(),req.files.pic.name,"w",{chunkSize:1024,metadata:{}})
//console.dir('testing');
});
app.get('/image/:id',function(req,res){
var id = req.params.id;
var ObjectId = require('mongodb').ObjectID;
var outPutFromDbFile = __dirname + '/public/uploads/' + id + '.png';
var writeStream = fs.createWriteStream(outPutFromDbFile);
// var BSON = require('mongodb').BSONPure;
// var o_id = BSON.ObjectID.createFromHexString(id);
var o_id = ObjectID(id);
var gridStore = new GridStore(db,o_id,"r");
gridStore.open(function (err,gridStore){
if(err)
{
console.log('error' + err);
}
var readStream = gridStore.stream(true);
readStream.on("end",function(){
console.log('close was called');
res.sendFile(outPutFromDbFile);
});
readStream.pipe(writeStream);
});
});
app.listen(3000);
錯誤只發生在一些時間。所以我不知道是什麼原因造成的。任何幫助,將不勝感激。
我在解決方案中使用了這個,非常感謝。 – onemillion