--- title: FileReader slug: Web/API/FileReader tags: - API - File API - Files - Interface - NeedsTranslation - Reference - TopicStub translation_of: Web/API/FileReader ---
{{APIRef("File API")}}

Objektet FileReader låter webbapplikationer asynkroniserat läsa innehåll i filer sparade på användarens dator. Man använder objekten {{domxref("File")}} eller {{domxref("Blob")}} för att specificera filerna eller datan som ska läsas.

Objekt av typen File kan man få av från ett FileList-objekt till följd av att användaren har valt en fil med {{HTMLElement("input")}}, en drag-och-släpp-operations DataTransfer-objekt, eller från API:et mozGetAsFile() på ett {{domxref("HTMLCanvasElement")}}.

{{AvailableInWorkers}}

Konstruktor

{{domxref("FileReader.FileReader", "FileReader()")}}
Returnerar en nykonstruerad FileReader.

Se Using files from web applications för detaljer och exempel (på engelska).

Egenskaper

{{domxref("FileReader.error")}} {{readonlyinline}}
Ett {{domxref("DOMException")}} som representerar ett fel som uppstod vid inläsning av filen.
{{domxref("FileReader.readyState")}} {{readonlyinline}}
Ett nummer som indikerar vilket stadie FileReader-objektet är i. Följande värden finns:
EMPTY 0 Ingen data har laddats ännu.
LOADING 1 Datan laddas just nu.
DONE 2 Hela läsförfrågningen är klar.
{{domxref("FileReader.result")}} {{readonlyinline}}
Filens innehåll. Denna egenskap är endast giltlig när operationen är färdig. Formatet på datan beror på vilken metod som användes för att initiera läsoperationen.

Händelsehanterare

{{domxref("FileReader.onabort")}}
Hanterar händelsen {{event("abort")}}. Denna händelse aktiveras varje gång läsoperationen avbryts.
{{domxref("FileReader.onerror")}}
A handler for the {{event("error")}} event. This event is triggered each time the reading operation encounter an error.
{{domxref("FileReader.onload")}}
A handler for the {{event("load")}} event. This event is triggered each time the reading operation is successfully completed.
{{domxref("FileReader.onloadstart")}}
A handler for the {{event("loadstart")}} event. This event is triggered each time the reading is starting.
{{domxref("FileReader.onloadend")}}
A handler for the {{event("loadend")}} event. This event is triggered each time the reading operation is completed (either in success or failure).
{{domxref("FileReader.onprogress")}}
A handler for the {{event("progress")}} event. This event is triggered while reading a {{domxref("Blob")}} content.

As FileReader inherits from {{domxref("EventTarget")}}, all those events can also be listened for by using the {{domxref("EventTarget.addEventListener()","addEventListener")}} method.

Methods

{{domxref("FileReader.abort()")}}
Aborts the read operation. Upon return, the readyState will be DONE.
{{domxref("FileReader.readAsArrayBuffer()")}} {{gecko_minversion_inline("7.0")}}
Starts reading the contents of the specified {{domxref("Blob")}}, once finished, the result attribute contains an {{domxref("ArrayBuffer")}} representing the file's data.
{{domxref("FileReader.readAsBinaryString()")}}
Starts reading the contents of the specified {{domxref("Blob")}}, once finished, the result attribute contains the raw binary data from the file as a string.
{{domxref("FileReader.readAsDataURL()")}}
Starts reading the contents of the specified {{domxref("Blob")}}, once finished, the result attribute contains a data: URL representing the file's data.
{{domxref("FileReader.readAsText()")}}
Starts reading the contents of the specified {{domxref("Blob")}}, once finished, the result attribute contains the contents of the file as a text string.

Specifications

Specification Status Comment
{{SpecName("File API", "#dfn-filereader", "FileReader")}} {{Spec2("File API")}} Initial definition

Browser compatibility

{{Compat("api.FileReader")}}

See also