Can we configure multer to not store files locally and only for using req.files.image.path?
July 2018:
So if you want to store an image into some sort of database, you probably want to convert it into a buffer, and then insert buffer into the database. If you are using multer
to process the multipart form (in this case the image file you want to upload), you can use multer
's memoryStorage
to get the buffer of the image directly. In other words:
var storage = multer.memoryStorage(); var upload = multer({ storage: storage });
to get the buffer:
When using memory storage, the file info will contain a field called buffer that contains the entire file.
that means you can get the buffer from command like req.files[0].buffer
I updated a simple repo demonstrating upload images to database without making a copy to local in this repo
From Multer info page athttps://www.npmjs.com/package/multer
If the inMemory option is true - no data is written to disk but data is kept in a buffer accessible in the file object.
Prior to 14-07-2016 you could not configure multer
to store files locally.
But multer is just a wrapper of busboy. So, we can try use busboy
directly if we want to avoid hitting the disk.
Connect-bubsboy will help us:
app.use(busboy());// request handlerreq.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { file.on('end', function() { // do stuff with req.files[fieldname] });});
Update 14-07-2016
Now multer has MemoryStorage
to store files in memory as Buffer.
var storage = multer.memoryStorage()var upload = multer({ storage: storage })