javascript - convert blob to text (from utf-8 bytes)

JavaScript
[Edit]
+
0
-
0

JavaScript - convert Blob to text (from UTF-8 bytes)

1 2 3 4 5 6 7 8 9 10
const blob = ... // Hint: File is Blob so we can convert read file as text. const text = await blob.text(); // Note: Blob's text() method was introduced around 2019-2020 in the major web browsers. // See also: // // 1. https://dirask.com/snippets/JavaScript-convert-text-to-Blob-text-plain-and-UTF-8-encoding-DlBN4j
[Edit]
+
0
-
0

JavaScript - convert blob to text (from UTF-8 bytes)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
function readText(blob, callback) { if (window.FileReader) { var reader = new FileReader(); reader.onload = function() { callback(reader.result, null); }; reader.onerror = function() { callback(null, 'Incorrect blob or file object.'); }; reader.readAsText(blob, 'UTF-8'); } else { callback(null, 'File API is not supported.'); } } // Usage example: const blob = ... // Hint: File is Blob so we can convert read file as text. readText(blob, function(text, error) { if (!error) { // Put `text` variable usage here ... } }); // See also: // // 1. https://dirask.com/snippets/JavaScript-convert-text-to-Blob-text-plain-and-UTF-8-encoding-DlBN4j
[Edit]
+
0
-
0

JavaScript - convert blob to text (from UTF-8 bytes)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
const readText = async (blob) => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result); reader.onerror = () => reject('Incorrect blob or file object.'); reader.readAsText(blob, 'UTF-8'); }); }; // Usage example: const blob = ... // Hint: File is Blob so we can convert read file as text. const text = await readText(blob); // See also: // // 1. https://dirask.com/snippets/JavaScript-convert-text-to-Blob-text-plain-and-UTF-8-encoding-DlBN4j