1

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 }
0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
What about the issue of the 'reset' due to 'nodemon'? github.com/remy/nodemon/issues/1509 does this approach not restart node because it is 'watching' for file changes?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.