我正在尝试使用gridfs-stream创建一个writestream,然后将其通过管道传输到文件系统中。我无法使用fs.createReadStream将其通过管道传输到文件系统中,因为我没有提供正确的文件路径。我想从表单获取文件路径。错误如下:
我想将正确的文件路径放入fs.createReadStream(INPUT FILE PATH HERE).pipe(writestream)
req.file.path
不起作用
req.files.path
不起作用
我无法找到正确的req.file.(something)
来指定文件路径。
/Users/ronfranz/Projects/Library/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^
TypeError: Cannot read property 'path' of undefined
at NativeConnection.<anonymous> (/Users/ronfranz/Projects/Library/library.js:181:25)
at Object.onceWrapper (events.js:273:13)
at NativeConnection.emit (events.js:182:13)
at /Users/ronfranz/Projects/Library/node_modules/mongoose/lib/connection.js:573:13
at result (/Users/ronfranz/Projects/Library/node_modules/mongoose/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/Users/ronfranz/Projects/Library/node_modules/mongoose/node_modules/mongodb/lib/utils.js:406:9)
at err (/Users/ronfranz/Projects/Library/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:286:5)
at connectCallback (/Users/ronfranz/Projects/Library/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:241:5)
at process.nextTick (/Users/ronfranz/Projects/Library/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:463:7)
at process._tickCallback (internal/process/next_tick.js:61:11)
[nodemon] app crashed - waiting for file changes before starting...
var express = require('express');
var exphbs = require('express-handlebars');
var session = require('express-session');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var multer = require('multer');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo
var storage = require('multer-gridfs-storage')({
url: 'mongodb://localhost:27017/YourDB'
});
var upload = multer({ storage: storage });
/* FOR USE W/ MULTER-GRID-FS STORAGE - CURRENTLY USING GRIDFS-STREAM
var storage = require('multer-gridfs-storage')({
db: connection
file: (req, file) => {
return {
bucketName: 'Books',
filename: 'file_' + Date.now()
};
}
});
var upload = multer({ storage: storage });
*/
var bodyParser = require('body-parser');
var bcrypt = require('bcrypt');
var app = express();
const fs = require('fs');
// set up handlebars view engine
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
var port = process.env.port || 8080;
app.listen(port);
app.use(express.static(__dirname + '/public'));
// Setting up cookie parser
app.use(session({
secret: 'i am the greatest secret of the modern world',
resave: true,
saveUninitialized: false
}));
//Uploading files
app.post('/uploadFile/:title', upload.single('file'), function(req, res){
var conn = mongoose.createConnection("mongodb://localhost:27017/YourDB");
//Making sure the db instance is open before passing into 'Grid'
conn.once('open', function(){
var gfs = Grid(conn.db);
console.log(req.files.path);
// Creating write steam to stream data to GridFs
var writestream = gfs.createWriteStream({
_id: req.file.id,
filename: req.file.filename,
mode: 'w',
});
fs.createReadStream(req.files.path).pipe(writestream);
writestream.on('close', function(file){
console.log('succesfully uploaded to the database!');
});
res.render('bookshelf');
});
});
这是下面的表格
<form id="uploadLinker" action="/uploadFile/The_Lord_of_the_Rings" method="POST" enctype="multipart/form-data">
<input type="file" name="file" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>