0

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:
   <Buffer ff d8 ff e1 ... >,
  size: 5173060 }
AlexZeDim
  • 3,520
  • 2
  • 28
  • 64
Arventil
  • 23
  • 1
  • 1
  • 4

2 Answers2

1

TL:DR via fs module

const fs = require('fs');
const data = { fieldname: 'file',
  originalname: '20190221_171825.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer:
   <Buffer ff d8 ff e1 ... >,
  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.

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64
  • 1
    Thank you (also for pointing to similar thread). It helped me a lot! I must made some kind of mistake earlier, because i tried to follow steps i found on stackoverflow and it didn't work. Now its working great. ;) But there is one thing that you should change in your answer - constant named "data" should contain only value of buffer, not whole file object. – Arventil Sep 02 '19 at 18:14
  • What about the issue of the 'reset' due to 'nodemon'? https://github.com/remy/nodemon/issues/1509 does this approach not restart node because it is 'watching' for file changes? – Vass Apr 15 '21 at 17:40
0

You can use JIMP module to move or manipulate your image.

YanDevDz
  • 1
  • 1