Class FileReader
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The FileReader interface 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.
[Value("FileReader")]
public class FileReader : EventTarget
- Inheritance
-
FileReader
- Inherited Members
Remarks
File objects may be obtained from a FileList object returned as a result of a user selecting files using the <input type="file"> element, or from a drag and drop operation's DataTransfer object. FileReader can only access the contents of files that the user has explicitly selected; it cannot be used to read a file by pathname from the user's file system. To read files on the client's file system by pathname, use the File System Access API. To read server-side files, use Windowfetch, with CORS permission if reading cross-origin.
Constructors
FileReader()
The FileReader() constructor creates a new FileReader.
public FileReader()
Remarks
For details about how to use FileReader, see Using files from web applications.
Fields
DONE
[Value("DONE")]
public const ushort DONE = 2
Field Value
EMPTY
[Value("EMPTY")]
public const ushort EMPTY = 0
Field Value
LOADING
[Value("LOADING")]
public const ushort LOADING = 1
Field Value
Properties
Error
The error read-only property of the FileReader interface returns the
error that occurred while reading the file.
[Value("error")]
public DOMException? Error { get; }
Property Value
- DOMException
A DOMException containing the relevant error.
Remarks
Onabort
[Value("onabort")]
public EventHandlerNonNull Onabort { get; set; }
Property Value
Onerror
[Value("onerror")]
public EventHandlerNonNull Onerror { get; set; }
Property Value
Onload
[Value("onload")]
public EventHandlerNonNull Onload { get; set; }
Property Value
Onloadend
[Value("onloadend")]
public EventHandlerNonNull Onloadend { get; set; }
Property Value
Onloadstart
[Value("onloadstart")]
public EventHandlerNonNull Onloadstart { get; set; }
Property Value
Onprogress
[Value("onprogress")]
public EventHandlerNonNull Onprogress { get; set; }
Property Value
ReadyState
The readyState read-only property of the FileReader interface provides the current state of the reading operation.
This will be one of the states: EMPTY, LOADING, or DONE.
[Value("readyState")]
public ushort ReadyState { get; }
Property Value
- ushort
A number which is one of the three possible state constants defined on the FileReader interface:
Remarks
Result
The result read-only property of the FileReader interface returns 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.
[Value("result")]
public Union45? Result { get; }
Property Value
- Union45?
An appropriate string or ArrayBuffer based on which of the reading methods
was used to initiate the read operation. The value isnullif the reading
is not yet complete or was unsuccessful.The result types are described below.
Remarks
Methods
Abort()
The abort() method of the FileReader interface aborts the read operation. Upon return,
the ReadyState will be DONE.
[Value("abort")]
public GlobalObject.Undefined Abort()
Returns
Remarks
ReadAsArrayBuffer(Blob)
The readAsArrayBuffer() method of the FileReader interface is used to start reading the
contents of a specified Blob or File. When the read
operation is finished, the ReadyState property becomesDONE, and the FileReaderloadend event is
triggered. At that time, the Result property
contains an ArrayBuffer representing the file's data.
[Value("readAsArrayBuffer")]
public GlobalObject.Undefined ReadAsArrayBuffer(Blob blob)
Parameters
blobBlob
Returns
Remarks
NOTE
The ArrayBuffer() method is a newer promise-based API to read a
file as an array buffer.
ReadAsBinaryString(Blob)
IMPORTANT
DeprecatedNOTE
This method is deprecated in favor of ReadAsArrayBuffer(Blob).
[Value("readAsBinaryString")]
public GlobalObject.Undefined ReadAsBinaryString(Blob blob)
Parameters
blobBlob
Returns
Remarks
The readAsBinaryString() method of the FileReader interface is used to start reading the contents of the
specified Blob or File. When the read operation is
finished, the ReadyState property becomesDONE, and the FileReaderloadend event is triggered. At that time, the
Result property contains the raw binary data from
the file.
Note that this method was once removed from the File API specification, but
re-introduced for backward compatibility.
Using ReadAsArrayBuffer(Blob) is recommended.
ReadAsDataURL(Blob)
The readAsDataURL() method of the FileReader interface is used to read the contents of the specified
Blob or File. When the read operation is finished, the
ReadyState property becomes DONE, and the
FileReaderloadend event is triggered. At that time, the
Result attribute contains the data as a data: URL representing the
file's data as a base64 encoded string.
[Value("readAsDataURL")]
public GlobalObject.Undefined ReadAsDataURL(Blob blob)
Parameters
blobBlob
Returns
Remarks
NOTE
The blob's Result cannot be
directly decoded as Base64 without first removing the Data-URL declaration preceding
the Base64-encoded data. To retrieve only the Base64 encoded string, first
removedata:*/*;base64,from the result.
ReadAsText(Blob, string)
The readAsText() method of the FileReader interface is used to read the contents of the specified Blob or File.
When the read operation is complete, the ReadyState property is changed to DONE,
the FileReaderloadend event is triggered, and the Result property contains the contents of the file as a text string.
[Value("readAsText")]
public GlobalObject.Undefined ReadAsText(Blob blob, string encoding = null)
Parameters
Returns
Remarks
NOTE
The 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 ReadAsArrayBuffer(Blob) for large files.