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 :

ajp
  • 11
  • 2
  • It's very hard to say. What is from returning? Can you check its type? Can you list its methods? – Ricardo Peres Apr 07 '20 at 20:05
  • Code in the post *looks* copied from totally wrong (how to read text in nodeJS) sample... https://stackoverflow.com/questions/51511307/nodejs-conversion-from-buffer-data-to-byte-array looks more related... – Alexei Levenkov Apr 07 '20 at 20:25

0 Answers0