FileReader
Objektet FileReader
låter webbapplikationer asynkroniserat läsa innehåll i filer sparade på användarens dator. Man använder objekten File
eller 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 <input>
, en drag-och-släpp-operations DataTransfer-objekt
, eller från API:et mozGetAsFile()
på ett HTMLCanvasElement
.
Note:
This feature is available in Web Workers.Konstruktor
FileReader()
- Returnerar en nykonstruerad
FileReader
.
Se Using files from web applications för detaljer och exempel (på engelska).
Egenskaper
FileReader.error
Read only- Ett
DOMException
som representerar ett fel som uppstod vid inläsning av filen. FileReader.readyState
Read only- 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. FileReader.result
Read only- 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
FileReader.onabort
- Hanterar händelsen
abort
. Denna händelse aktiveras varje gång läsoperationen avbryts. FileReader.onerror
- A handler for the
error
event. This event is triggered each time the reading operation encounter an error. FileReader.onload
- A handler for the
load
event. This event is triggered each time the reading operation is successfully completed. FileReader.onloadstart
- A handler for the
loadstart
event. This event is triggered each time the reading is starting. FileReader.onloadend
- A handler for the
loadend
event. This event is triggered each time the reading operation is completed (either in success or failure). FileReader.onprogress
- A handler for the
progress
event. This event is triggered while reading aBlob
content.
As FileReader
inherits from EventTarget
, all those events can also be listened for by using the addEventListener
method.
Methods
FileReader.abort()
- Aborts the read operation. Upon return, the
readyState
will beDONE
. FileReader.readAsArrayBuffer()
- Starts reading the contents of the specified
Blob
, once finished, theresult
attribute contains anArrayBuffer
representing the file's data. FileReader.readAsBinaryString()
- Starts reading the contents of the specified
Blob
, once finished, theresult
attribute contains the raw binary data from the file as a string. FileReader.readAsDataURL()
- Starts reading the contents of the specified
Blob
, once finished, theresult
attribute contains adata:
URL representing the file's data. FileReader.readAsText()
- Starts reading the contents of the specified
Blob
, once finished, theresult
attribute contains the contents of the file as a text string.
Specifications
Specification | Status | Comment |
---|---|---|
File API The definition of 'FileReader' in that specification. |
Working Draft | Initial definition |
Browser compatibility
BCD tables only load in the browser