Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda