I'm trying to save a file to a specific location on the server disk (using multer), but this location name is related to data i receive in request along with file.
I came to conclusion that I can save file in memory, and later (after some other part of code will complete, and i will have my location name generated) I will save that file to disk space. And this is where i stuck - how can I save file in node.js from object in memory to specific disk location?
This is object that i have saved in memory:
{ fieldname: 'file',
originalname: '20190221_171825.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer:
,
size: 5173060 }
Answer
TL:DR via fs module
const fs = require('fs');
const data = { fieldname: 'file',
originalname: '20190221_171825.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer:
,
size: 5173060 };
fs.writeFile("path/to/file", data, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
But to be honest, you should check other relevant questions before asking a new one here.
No comments:
Post a Comment