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

281
Views
Compruebe si el objeto de archivo/blob es UTF-8 válido

Necesito una función que pueda verificar si un archivo o un objeto blob es UTF-8 válido. Puedo obtener el texto y verificar los caracteres �, pero si la cadena tiene ese carácter para comenzar, la función lo marcaría como inválido.

 function isUTF8(blob) { return new Promise(async resolve => { const text = await blob.text(); resolve(!~text.indexOf("�")); }); } // "�" is valid utf-8 but the function returns false isUTF8(new Blob(["�"])).then(console.log); // returns true isUTF8(new Blob(["example"])).then(console.log);

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede utilizar la API de TextDecoder :

 async function isUTF8(blob) { const decoder = new TextDecoder('utf-8', { fatal: true }); const buffer = await blob.arrayBuffer(); try { decoder.decode(buffer); } catch (e) { if (e instanceof TypeError) return false; throw e; } return true; } (async () => { console.log(await isUTF8(new Blob( [new Uint8Array([0x80])]))); // false console.log(await isUTF8(new Blob( [new Uint8Array([0xef, 0xbf, 0xbd])]))); // true console.log(await isUTF8(new Blob( ["\ufffd"]))); // true console.log(await isUTF8(new Blob( ["example"]))); // true })().catch(e => console.warn(e));

Lo anterior carga todo el Blob en un ArrayBuffer por simplicidad. Si la eficiencia de la memoria se convierte en un problema, puede considerar usar el método .stream() para procesar el Blob en partes, sin mantenerlo en la memoria en su totalidad.

about 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!