Я пытаюсь с помощью gridfs-stream создать поток записи и затем направить его в файловую систему. Я не могу передать его в файловую систему с помощью 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>