Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadFile in Base64 Nodejs

I'm trying to read an image from client side encoded in base64. How to read with nodejs?

My code:

// add to buffer base64 image var encondedImage = new Buffer(image.name, 'base64');  fs.readFile(encondedImage, "base64", function(err, buffer){     if ( err ) {         console.log('In read file')         console.log(err)     } else {         // check err         lwip.open(buffer, 'jpg', function(err, image){             console.log('in open')             if ( err ) console.log(err)              if ( image ) console.log(image)             // check 'err'. use 'image'.             // image.resize(...), etc.         });     } }) 

But, I got this error:

In read file [Error: Path must be a string without null bytes.] 
like image 973
brunocascio Avatar asked Mar 03 '15 14:03

brunocascio


1 Answers

Latest and greatest way to do this:

Node supports file and buffer operations with the base64 encoding:

const fs = require('fs'); const contents = fs.readFileSync('/path/to/file.jpg', {encoding: 'base64'}); 

Or using the new promises API:

const fs = require('fs').promises; const contents = await fs.readFile('/path/to/file.jpg', {encoding: 'base64'}); 
like image 128
Mike Martin Avatar answered Sep 22 '22 08:09

Mike Martin