10

I am trying to convert an image file into base64, so I can store in base64 string form in mongoDB.

This is how I am trying to do that:

router.post('/file_upload',function(req,res){

  function base64_encode(file) {
    var bitmap = fs.readFileSync(file);
    return new Buffer(bitmap).toString('base64');
}

  var ImageFileToSave =  base64_encode(req.body.file);

  console.log(ImageFileToSave);


})

On Client side:

<form action="/file_upload" method="POST" enctype="multipart/form-
 data">
<input type="file" name="file" />
<input type="submit" value="Upload File" />
</form>

This is the error that I am getting

TypeError: path must be a string or Buffer

how can I convert that image file(eg:image.jpg) into base64?

7
  • What is the data type of req.body.file? Commented Feb 13, 2018 at 8:14
  • it is buffer i think Commented Feb 13, 2018 at 8:15
  • Btw, where do you get this error? client or server? Commented Feb 13, 2018 at 8:16
  • on server side. Commented Feb 13, 2018 at 8:17
  • 1
    Ok. Kindly check the value of req.body.file and its data type. Thanks. Commented Feb 13, 2018 at 8:18

3 Answers 3

19

You will need to use Multer middleware to handle multipart/form-data.

const express = require('express')
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/file_upload', upload.single('example'), (req, res, next) => {
  // req.file is the `example` file or whatever you have on the `name` attribute: <input type="file" name="example" />
  // I believe it is a `Buffer` object.
  const encoded = req.file.buffer.toString('base64')
  console.log(encoded)
})

2018-10-24: See David's comment below.

2019-06-11: Fixed example based on comments. It is indeed req.file.buffer: https://github.com/expressjs/multer/blob/master/storage/memory.js#L8

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

2 Comments

It didn't work to me, but with a little change it worked const encoded = req.file.buffer.toString('base64');
Not sure if it was like this in older versions of Multer, but for me it worked with req.file.buffer.toString('base64') too. Thank you. :)
7

As the previous answer wasn't working for me, I'm sharing this other one that worked. I made with the multer library getting the file and then transforming it to base64

const multer  = require('multer')
const upload = multer({});

router.post('/uploadlogo', upload.single('logo'), (req, res, next) => {
    // encoded has the base64 of your file
    const encoded = req.file.buffer.toString('base64');
});

Comments

0

you can also do it manually in a way if you want more control. here is an example but you can implement it in any way you want, as middleware or as a function to call alone.

this is assuming your image is base64 which would be "req.body.image".


var ImageMiddleware =  (req, res, next)=> {
  if (req.body.image && req.body.foldername && req.body.filename){
    const imagepath = req.body.foldername + "/" + uuidv4() + "_" + req.body.filename;

      function buff(data) {
        const base64 = data.split(',')[1];
        let buff = Buffer.from(base64, 'base64');
        return buff
      }

      fs.writeFileSync(imagepath, buff(req.body.image))

  }
}


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.