FileReader

Den här översättning är ofullständig. Snälla hjälp oss att översätta denna artikel från engelska

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 a Blob 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 be DONE.
FileReader.readAsArrayBuffer()
Starts reading the contents of the specified Blob, once finished, the result attribute contains an ArrayBuffer representing the file's data.
FileReader.readAsBinaryString()
Starts reading the contents of the specified Blob, once finished, the result attribute contains the raw binary data from the file as a string.
FileReader.readAsDataURL()
Starts reading the contents of the specified Blob, once finished, the result attribute contains a data: URL representing the file's data.
FileReader.readAsText()
Starts reading the contents of the specified Blob, once finished, the result 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

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
FileReaderChrome Full support 7Edge Full support 12Firefox Full support 3.6
Notes
Full support 3.6
Notes
Notes Prior to Firefox 4, Blob parameters were File parameters.
IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support 1.0
abortChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
abort eventChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
errorChrome Full support 7Edge Full support 12Firefox Full support 3.6
Notes
Full support 3.6
Notes
Notes Prior to Firefox 13, the error property returned a FileError object.
Notes From Firefox 13 to Firefox 58, the error property returned a DOMError object.
Notes From Firefox 58, the error property returns a DOMException object.
IE Full support 10
Notes
Full support 10
Notes
Notes The error property returns a DOMError object.
Opera Full support 11Safari Full support 6
Notes
Full support 6
Notes
Notes The error property returns a DOMError object.
WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32
Notes
Full support 32
Notes
Notes From Firefox 32 to Firefox 58, the error property returned a DOMError object.
Notes From Firefox 58, the error property returns a DOMException object.
Opera Android Full support 11Safari iOS Full support 6.1
Notes
Full support 6.1
Notes
Notes The error property returns a DOMError object.
Samsung Internet Android Full support Yes
error eventChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
load eventChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
loadend eventChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
loadstart eventChrome Full support YesEdge Full support 12Firefox Full support 79
Full support 79
No support ? — 79
Notes
Notes loadstart event dispatches synchronously (should be asynchronously as per spec).
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 79
Full support 79
No support ? — 79
Notes
Notes loadstart event dispatches synchronously (should be asynchronously as per spec).
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
onabortChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
onerrorChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
onloadChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support 1.0
onloadendChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
onprogressChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
progress eventChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
readAsArrayBufferChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 12Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 32Opera Android Full support 12Safari iOS Full support 6.1Samsung Internet Android Full support 1.0
readAsBinaryStringChrome Full support 7Edge Full support 12Firefox Full support 3.6IE No support NoOpera Full support 11Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support 1.0
readAsDataURLChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 32
Notes
Full support 32
Notes
Notes Using the camera in Android 8.x raises an exception. See bug 1511083.
Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support 1.0
readAsTextChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support 1.0
readyStateChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
resultChrome Full support 7Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 11Safari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 32Opera Android Full support 11Safari iOS Full support 6.1Samsung Internet Android Full support Yes
Available in workersChrome Full support YesEdge Full support ≤18Firefox Full support 46IE No support NoOpera Full support 11Safari No support NoWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 46Opera Android Full support 11Safari iOS No support NoSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

See also