Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Node.JS JavaScript buffer to image conversion not a valid bitmap file

I'm trying to transfer an image file data from one node.js script to another.

I read the image file in 'parameter.js' using 'readfilesync', then transfer it to another script 'convert.js'.

convert.js is meant to re-convert the file data back to an image using WriteFile - and it seems to do a good job.

However, when I try to open the newly created file, I get the error like;

This is not a valid bitmap file, or its format is not currently supported.

Could you help me spot the problem?

Thanks for your help!

parameter.js:


// Path to image file
let datasetRoute = resolve('./app/scripts/myphotos/bubbles1.png');

// Get image file
let file = readDataset();

// Function to get image file
function readDataset() {

    try { 

        // Fetch dataset using new 'currentPage' number
        return fs.readFileSync(datasetRoute,  'utf8');
    }

    catch (err) { 
        return err;
    }
}

// Create parameter function
const parameterFunctions = {

    // Define function parameters
    
    // Parameter 1
    parameter1: file, // set parameter1 as value of file

}

convert.js:


var bytes = params.uParams[0].parameter1.replace(/^data:image\/png;base64,/, "");

fs.writeFileSync('app/scripts/media/test.png', bytes, 'base64', (err) => {
    if (!err) 
    console.log(`Image saved!`);
});

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Converted the entire operation into a simple read and write 'binary' format.

Finally worked!

Here's the code:

parameter.js:



// Path to image file
let datasetRoute = resolve('./app/scripts/myphotos/bubbles1.png');

// Get image file
let file = readDataset();

// Function to get image file
function readDataset() {

    try { 

        // Fetch dataset using new 'currentPage' number
        return fs.readFileSync(datasetRoute, 'binary');
    }

    catch (err) { 
        return err;
    }
}

// Create parameter function
const parameterFunctions = {

    // Define function parameters
    
    // Parameter 1
    parameter1: file, // set parameter1 as value of file

}

convert.js:


var bytes = params.uParams[0].parameter1;

fs.writeFileSync('app/scripts/media/test.png', bytes, 'binary', (err) => {
    if (!err) 
    console.log(`Image saved!`);
});

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!