1

I have to call a c# api which expects a byte array as one of the property in json.

public byte[] payload { get; set; }
public int id { get; set; }
public string file_type { get; set; }

In postman, I am making this call and able to push my file. But I am not able to convert the file into byte array in Node Js. In c# this is how it is converted :

 byte[] bytes = System.IO.File.ReadAllBytes(@"C:\images\sample.jpg");

In node js I am trying this approach :

fs.readFile('sample.jpg', 'utf8', function(err, data){ 

      // Display the file content 
      //console.log(data); 
      var convertedBuffer = Buffer.from(data, "utf-8");

      var byteArray = convertedBuffer.alloc(8);
      console.log(byteArray); 
  }); 

I am getting an error convertedBuffer.alloc is not a function. What is the wrong in this code please?

Postman Request image :

2
  • It's very hard to say. What is from returning? Can you check its type? Can you list its methods? Commented Apr 7, 2020 at 20:05
  • Code in the post looks copied from totally wrong (how to read text in nodeJS) sample... stackoverflow.com/questions/51511307/… looks more related... Commented Apr 7, 2020 at 20:25

0

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.