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

153
Views
Store large amount of data in Javascript

Im receiving some file chunks in bytes format from my server and im collecting them into one variable in my frontend to download it later. And I can't change my server conception (receiving a file sliced into chunks).

My problem is that if the file is heavy (from 500MB), my variable length is starting to be very very big and im having an error :

RangeError: Invalid string length

This is because my variable has reach the limit of character (536 800 000).

Here's how im adding my data to my variable :

this.socket.on('new_file', (data: string) => {
  this.receivedFile += data;
}

My download part :

public download(): void {
    const byteCharacters = this.receivedFile;
    const byteArrays = [];
    const sliceSize=512

    for (let offset = 0; offset < byteCharacters.length; offset += 
     sliceSize) {
      const slice = byteCharacters.slice(offset, offset + sliceSize);

      const byteNumbers = new Array(slice.length);
      for (let i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }

      const byteArray = new Uint8Array(byteNumbers);
      byteArrays.push(byteArray);
    }

    const blob = new Blob(byteArrays, {type: this.fileInfos.type});

    saveAs(blob, this.fileInfos.name);
  }

Which approach can I do ? Or does it exist some variable type in Javascript to accept more char ? Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Don't collect the chunks into a huge string. Instead, just convert each chunk into a bytearray immediately (that you'll need later anyway) and collect these:

this.socket.on('new_file', (data: string) => {
  const bytes = new Uint8Array(data.length);
  for (let i = 0; i < data.length; i++) {
    bytes[i] = data.charCodeAt(i);
  }
  this.byteArrays.push(bytes);
}

then

public download(): void {
  const blob = new Blob(this.byteArrays, {type: this.fileInfos.type});
  saveAs(blob, this.fileInfos.name);
}

I don't think you need to make 512-byte-sized slices.

about 4 years ago · Juan Pablo Isaza Report

0

Does stringifying one character at a time and then concatenating the result work?

e.g.

var out="[";
  for(var indx=0;indx<data.length-1;indx++){
    out+=JSON.stringify(data[indx],null,4)+",";
  }
  out+=JSON.stringify(data[data.length-1],null,4)+"]";

Credit: https://dev.to/madhunimmo/json-stringify-rangeerror-invalid-string-length-3977

about 4 years ago · Juan Pablo Isaza 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!