1

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!`);
});

5
  • I'm not sure, but have you tried reading your png as 'base64' instead of 'utf8' encoding? Commented Dec 5, 2021 at 10:34
  • Thanks! But already tried that. Commented Dec 5, 2021 at 11:47
  • 1
    If you only need to perform a copy you might as well do a simple 'binary' read and write Commented Dec 5, 2021 at 12:54
  • @DadiBit Thanks a lot! Binary worked! Ignore earlier comment. I had forgotten to apply 'binary' configuration on the read script. Thanks again! Commented Dec 5, 2021 at 13:20
  • @DadiBit Fixed code is below. Thanks again. Commented Dec 5, 2021 at 13:30

1 Answer 1

1

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!`);
});

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

1 Comment

You can accept your answer in a few days, btw. This way it's marked as closed, and it can reach more people when googling similar stuff.

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.