Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  100] [ 3]  / answers: 1 / hits: 178669  / 9 Years ago, tue, march 3, 2015, 12:00:00

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.]

More From » node.js

 Answers
15

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'});

[#67588] Sunday, March 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;