Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
What is the correct way to send ArrayBuffer or Blob across and reconstruct them?

I've read through a dozen posts on how to convert between ArrayBuffer to Blob or to Uint8Array etc. before sending the data to the client side... but I can't seem to be able to get it to work at all. When I do get the data through, I was not able to reconstruct them back as a Blob before outputting it to a file..

const Blob = require('cross-blob');
const randomBytes = require('randombytes');

const buffer = randomBytes(1024); // Supposed to give me Buffer

The followings were the stuff I tried...

data = buffer;

^ gives me <Buffer 11 22 33 ...>

data = Uint8Array.from(buffer); 

^ gives me an array of integer, this looked the most promising? but when arrived to the client side, it became an object with indexes and byte value...

data = Uint8Array.from(buffer).buffer;

^ gives ArrayBuffer { byteLength: 1024}, when inspect it shows size: 2 and type: 'text/plain'...

data = new Blob(buffer, { type: 'application/octet-stream' });
data = new Blob([new Uint8Array(buffer, buffer.byteOffset, buffer.length)], { type: 'application/octet-stream' });
data = new Blob([Uint8Array.from(buffer)], { type: 'application/octet-stream' });

^ all these, when arrived to the client side also with size: 2 and type: 'text/plain'...

On the server side, I am running Express:

router.get('/test/*', function(req, res, next) {
  ...
  let data = myFunctionThatGeneratesData();
  res.send(data);

});

On the client side, I'm requesting it like this (Angular/TypeScript):

this.http.get('/test/random-bytes-array', {
  responseType: 'blob'  // also tried 'arraybuffer'
}).subscribe(data => {
  debugger;
  console.log(data);
});

I must be doing something wrong... I am trying to send multiple chunk of binary data over, either as an ArrayBuffer, Uint8Array or Blob (whatever works) and when arriving at the other end, combine them back into a Blob.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In Node.js, crypto.randomBytes returns a Buffer. This is the correct type to use for sending raw data to the client.

When using Express, it is important to set the correct content-type of the response using res.type(). However, when sending a Buffer, if the content-type header is not set in any other middleware, then express will use application/octet-stream by default.

When the parameter is a Buffer object, the method sets the Content-Type response header field to "application/octet-stream", unless previously defined.

router.get('/test/*', function(req, res, next) {
  ...
  let data = myFunctionThatGeneratesData();
  res.type('application/octet-stream').send(data);
  // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ might not be necessary
});

And lastly in Angular, using the responseType: 'blob' is correct:

this.http.get('/test/random-bytes-array', {
  responseType: 'blob'
}).subscribe(data => {
  console.log(data);
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!