files/en-us/web/api/filereader/readastext/index.md
{{APIRef("File API")}}{{AvailableInWorkers}}
The readAsText() method of the {{domxref("FileReader")}} interface is used to read the contents of the specified {{domxref("Blob")}} or {{domxref("File")}}.
When the read operation is complete, the {{domxref("FileReader.readyState","readyState")}} property is changed to DONE,
the {{domxref("FileReader/loadend_event", "loadend")}} event is triggered, and the {{domxref("FileReader.result","result")}} property contains the contents of the file as a text string.
[!NOTE] The {{domxref("Blob.text()")}} method is a newer promise-based API to read a file as text.
[!NOTE] This method loads the entire file's content into memory and is not suitable for large files. Prefer {{domxref("FileReader.readAsArrayBuffer", "readAsArrayBuffer()")}} for large files.
readAsText(blob)
readAsText(blob, encoding)
blob
encoding {{optional_inline}}
None ({{jsxref("undefined")}}).
<input type="file" />
<p class="content"></p>
const content = document.querySelector(".content");
const fileInput = document.querySelector("input[type=file]");
fileInput.addEventListener("change", previewFile);
function previewFile() {
const file = fileInput.files[0];
const reader = new FileReader();
reader.addEventListener("load", () => {
// this will then display a text file
content.innerText = reader.result;
});
if (file) {
reader.readAsText(file);
}
}
{{EmbedLiveSample("Examples", "100%", 240)}}
{{Specifications}}
{{Compat}}