Click here to Skip to main content
14,883,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Read binary file, encode it as base64 data and put this in json.
Display base64 in console.log (data.src) of readBinary function.
However, there is no src member in the resData JSON after calling the function.
How can I solve this?

What I have tried:

app.post('/server', function(req, res){
    var resData = {}; 

    /*return to resData JSON {num: 1, planet: 12, math: 234, int: 102}*/
    resData = prevInstance[num].getFile(num++);

    readBinary(resData, num);

    /*here no member src*/   
    console.log(resData);
    res.json(redData);
});

function readBinary(data, file_id){
    var fs = require('fs');            
    var bkgndBuffer = new Buffer.alloc(data.size, 'base64');   
        fs.open('projectfile.file', 'r', function(err, fd){
        fs.read(fd, bkgndBuffer, 0, size, position, function(){   
            /*adding 'src' member*/               
            data.src=bkgndBuffer.toString('base64');                       
            /* it's working. showing base64 data */
            console.log(data.src);
        });   
        fs.close(fd, function(){
            console.log("exit file read.");
        });                       
    }); 
}
Posted
Comments
Richard MacCutchan 1-Apr-21 3:30am
   
You need to return the string from your readBinary function.
Chopin2001 1-Apr-21 5:12am
   
This function return base64 value. this is wrong?
Could you show me example?
Thanks.
Richard MacCutchan 1-Apr-21 5:51am
   
Sorry, I do not understand. I thought you wanted it to create a base64 string.
Chopin2001 2-Apr-21 4:47am
   
OK. thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900