What is the right way to use MemoryStorage option?
See original GitHub issueHi.
I get an error with this code(Cannot read property 'buffer'
). What is the right way to use memoryStorage option?
// import and use the modules
var app = require('express')();
var http = require('http').Server(app);
var bodyParser = require("body-parser");
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/imagenes', upload.single(''), function(req,res){
req.file.buffer; // TypeError: Cannot read property 'buffer' of undefined
});
http.listen(8080,function(){
console.log("Listening: 8080");
});
Thanks.
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
multer.memoryStorage JavaScript and Node.js code ...
Best JavaScript code snippets using multer.memoryStorage(Showing top 5 results out of 315) ; multer.memoryStorage({ destination: function(req, file, callback) { ...
Read more >Uploading sensitive data, Should I user memoryStorage ...
The data will then be saved to mySQL. All is done in Node.js. Now I wonder what is the most secure way to...
Read more >Multer buffer is null when using memoryStorage
I like doing it this way so that I can have more control over the error message. var uploadFile = multer( { storage:...
Read more >Express multer middleware
MemoryStorage. The memory storage engine stores the files in memory as Buffer objects. It doesn't have any options. const ...
Read more >Apache Hadoop 3.3.4 – Memory Storage Support in HDFS
The choice of RAM Disk allows better data persistence across Data Node process restarts. The following setup will work on most Linux ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @LinusU var express = require(“express”) var multer = require(‘multer’) var app = express() var path = require(‘path’) var fs = require(‘fs’)
var ejs = require(‘ejs’) app.set(‘view engine’, ‘ejs’)
var MAGIC_NUMBERS = { jpg: ‘ffd8ffe0’, jpg1: ‘ffd8ffe1’, png: ‘89504e47’, gif: ‘47494638’ }
function checkMagicNumbers(magic) { if (magic == MAGIC_NUMBERS.jpg || magic == MAGIC_NUMBERS.jpg1 || magic == MAGIC_NUMBERS.png || magic == MAGIC_NUMBERS.gif) return true }
app.get(‘/api/file’, function(req, res) { res.render(‘index’) })
app.post(‘/api/file’, function(req, res) { var upload = multer({ storage: multer.memoryStorage() }).single(‘userFile’) upload(req, res, function(err) { var buffer = req.file.buffer var magic = buffer.toString(‘hex’, 0, 4) var filename = req.file.fieldname + ‘-’ + Date.now() + path.extname(req.file.originalname) if (checkMagicNumbers(magic)) { fs.writeFile(‘./uploads/’ + filename, buffer, ‘binary’, function(err) { if (err) throw err res.end(‘File is uploaded’) }) } else { res.end(‘File is no valid’) } }) })
var port = process.env.PORT || 8080 app.listen(port, function() { console.log('Node.js listening on port ’ + port) })
Can you please help me on this i am getting below error “TypeError: multer.memoryStorage is not a function”
I think that this line is your problem:
It shouldn’t be an empty string, but rather the name of the field that is going to be uploaded.
What does your form look like?