Click here to Skip to main content
15,743,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement a code that reads binary data from a binary file, encodes it in base64, and returns it as JSON.
But I can't get the binary data read from the file. The result is always undefined.
How can I solve this problem?

What I have tried:

var createObj = [];'/server', function(req, res){    
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-with");
    res.header("Access-Control-Allow-Methods", "GET,POST")

    let resData = {}; 
    var num = req.body.pID;   

    createObj = new addonJS.ClassA();  //NAPI
    resData = createObj.getFile(num);  //NAPI
    resData.src = readBinary(resData, num);   
    console.log(resData); // src = undefined

function readBinary(data, file_id){
    var fs = require('fs');
    var BufferData = new Buffer.alloc(data.size);'data/'+file_id', 'r', function(err, fd){            
        var read = fs.readSync(fd, BufferData, 0, data.size, data.imageAddress);       
        console.log(BufferData.toString('base64') // correct result.             
        return 'data:image/jpeg;base64, '+BufferData.toString('base64');  
        fs.close(fd, function(){
            console.log("close file");
Updated 3-Apr-21 8:04am

You should return the data to the caller of the function:
JavaScript Functions[^].
Share this answer
Chopin2001 3-Apr-21 10:19am    
I did it.
return value in functin like this.
return 'data:image/jpeg;base64, '+BufferData.toString('base64');

data.src = readBinary(data, id);
but it's same. undefined
thanks a lot.
Richard MacCutchan 3-Apr-21 10:50am    
Well whatever you did, we cannot guess what it was. Please use the Improve question link above, and show the actual code you tried, and the exact results.
Chopin2001 3-Apr-21 14:07pm    
Perhaps my question wasn't clear.
Chopin2001 3-Apr-21 12:00pm    
Wasn't my question clear?
I can't move the whole code (this is difficult), but I thought I posted my problem exactly.
Anyway, I solve this problem.
You can check it out in my comments.
Thanks for your time.
I see a first problem in your code:
function realBinary(data, file_id){
    var BufferData = new Buffer.alloc(data.size);
    const file = fs.createReadStream('data/'+file_id+'.pro/Project');'data/'+file_id', 'r', function(err, fd){            
    //                     ^ this quote should be removed
        var read = fs.readSync(fd, BufferData, 0, size, imageAddress);       
        console.log(BufferData.toString('base64') // correct result.             
        data.src = 'data:image/jpeg;base64, '+BufferData.toString('base64');        
        console.log(data.src) // correct value 
        fs.close(fd, function(){
            console.log("cloe fd");
   /* here data.src is undefined */

Professional programmer's editors have features especially tailored for programmers like indentation, parenthesis matching and syntax highlighting.
Notepad++ Home[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]

Indentation style - Wikipedia[^]
Share this answer
Chopin2001 3-Apr-21 11:55am    
Thanks Patrice T. It's does work. It's solve.
I think it's problem callback function.
Well I want to know more about this problem, so I need to find out more.
 var fs = require('fs');       
    var bkgndBuffer = new Buffer.alloc(data.size); 
    var fd = fs.openSync('data/'+file_id+'.pro/Project', 'r');                
    var read = fs.readSync(fd, bkgndBuffer, 0, data.size, data.imageAddress);   
    data.src='data:image/jpeg;base64, '+bkgndBuffer.toString('base64');     
    fs.close(fd, function(){
        console.log("cloe fd");
Patrice T 3-Apr-21 12:00pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Chopin2001 3-Apr-21 14:06pm    
OK I did.
Thanks for your advise.

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