FileReader

FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.

File objects may be obtained from a FileList object returned as a result of a user selecting files using the <input> element, from a drag and drop operation's DataTransfer object, or from the mozGetAsFile() API on an HTMLCanvasElement .

Important note: FileReader is used to read file content from the user's (remote) system in secure ways only. It cannot be used to simply read a file by pathname from a file system. To read files by pathname in JavaScript, standard Ajax solutions should be used to do server-side file reading, with CORS permission if reading cross-domain.

注意: 此特征可用于 Web 工作者 .

构造函数

FileReader()
Returns a newly constructed FileReader .

Using files from web applications for details and examples.

特性

FileReader.error 只读
A DOMException representing the error that occurred while reading the file.
FileReader.readyState 只读
A number indicating the state of the FileReader . This is one of the following:
EMPTY 0 No data has been loaded yet.
LOADING 1 Data is currently being loaded.
DONE 2 The entire read request has been completed.
FileReader.result 只读
The file's contents. This property is only valid after the read operation is complete, and the format of the data depends on which of the methods was used to initiate the read operation.

事件处理程序

FileReader.onabort
A handler for the abort event. This event is triggered each time the reading operation is aborted.
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 继承自 EventTarget , all those events can also be listened for by using the addEventListener 方法。

方法

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. An optional encoding name can be specified.

Events

监听这些事件使用 addEventListener() 或通过把事件监听器赋值给 on eventname 特性为此接口。

abort
Fired when a read has been aborted, for example because the program called FileReader.abort() .
Also available via the onabort 特性。
error
Fired when the read failed due to an error.
Also available via the onerror 特性。
load
Fired when a read has completed successfully.
Also available via the onload 特性。
loadend
Fired when a read has completed, successfully or not.
Also available via the onloadend 特性。
loadstart
Fired when a read has started.
Also available via the onloadstart 特性。
progress
Fired periodically as data is read.
Also available via the onprogress 特性。

规范

规范 状态 Comment
File API
The definition of 'FileReader' in that specification.
工作草案 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
FileReader Chrome 7 Edge 12 Firefox 3.6
3.6
Prior to Firefox 4, Blob parameters were File 参数。
IE 10 Opera 11 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android 1.0
abort Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
abort event Chrome Yes Edge 12 Firefox Yes IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
error Chrome 7 Edge 12 Firefox 3.6
3.6
Prior to Firefox 13, the error property returned a FileError 对象。
From Firefox 13 to Firefox 58, the error property returned a DOMError 对象。
From Firefox 58, the error property returns a DOMException 对象。
IE 10
10
error property returns a DOMError 对象。
Opera 11 Safari 6
6
error property returns a DOMError 对象。
WebView Android Yes Chrome Android Yes Firefox Android 32
32
From Firefox 32 to Firefox 58, the error property returned a DOMError 对象。
From Firefox 58, the error property returns a DOMException 对象。
Opera Android 11 Safari iOS 6.1
6.1
error property returns a DOMError 对象。
Samsung Internet Android Yes
error event Chrome Yes Edge 12 Firefox Yes IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
load event Chrome Yes Edge 12 Firefox Yes IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
loadend event Chrome Yes Edge 12 Firefox Yes IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
loadstart event Chrome Yes Edge 12 Firefox 79
79
不支持 ? — 79
loadstart event dispatches synchronously (should be asynchronously as per spec).
IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onabort Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
onerror Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
onload Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android 1.0
onloadend Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
onprogress Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
progress event Chrome Yes Edge 12 Firefox Yes IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
readAsArrayBuffer Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 12 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 32 Opera Android 12 Safari iOS 6.1 Samsung Internet Android 1.0
readAsBinaryString Chrome 7 Edge 12 Firefox 3.6 IE No Opera 11 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android 1.0
readAsDataURL Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 32
32
Using the camera in Android 8.x raises an exception. See bug 1511083 .
Opera Android 11 Safari iOS 6.1 Samsung Internet Android 1.0
readAsText Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android 1.0
readyState Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
result Chrome 7 Edge 12 Firefox 3.6 IE 10 Opera 11 Safari 6 WebView Android Yes Chrome Android Yes Firefox Android 32 Opera Android 11 Safari iOS 6.1 Samsung Internet Android Yes
Available in workers Chrome Yes Edge ≤18 Firefox 46 IE No Opera 11 Safari No WebView Android Yes Chrome Android Yes Firefox Android 46 Opera Android 11 Safari iOS No Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。

另请参阅

Blob