1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use webcore::value::{Value, Reference};
use webcore::try_from::TryInto;
use webapi::blob::IBlob;
use webapi::event_target::{IEventTarget, EventTarget};
/// The FileReader object lets web applications asynchronously read the contents of files
/// (or raw data buffers) stored on the user's computer, using [File](struct.File.html)
/// or [Blob](struct.Blob.html) objects to specify the file or data to read.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader)
pub struct FileReader( Reference );
impl IEventTarget for FileReader {}
reference_boilerplate! {
FileReader,
instanceof FileReader
convertible to EventTarget
}
/// The result of a read operation performed with a [FileReader](struct.File.html).
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ReaderResult {
Text( String )
}
/// A number indicating the state of the `FileReader`.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readyState)
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ReadyState {
Empty,
Loading,
Done
}
impl FileReader {
/// Returns a newly constructed `FileReader`.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/FileReader)
pub fn new() -> FileReader {
js!( return FileReader(); ).try_into().unwrap()
}
/// Starts reading the contents of the specified blob, once finished, the result
/// attribute contains the contents of the file as a text string.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader)
pub fn read_as_text< T: IBlob >( &self, blob: &IBlob ) {
js!( @{self}.readAsText( @{blob.as_ref()} ); );
}
/// Aborts the read operation. Upon return, the `ready_state` will be `Done`.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/abort)
pub fn abort( &self ) {
js!( return @{self}.abort(); );
}
/// Returns the current state of the reader.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readyState)
pub fn ready_state( &self ) -> ReadyState {
let state: i32 = js!( return @{self}.readyState; ).try_into().unwrap();
match state {
0 => ReadyState::Empty,
1 => ReadyState::Loading,
2 => ReadyState::Done,
_ => unreachable!( "Unexpected value of FileReader::readyState: {}", state )
}
}
/// The file's contents. This method will only return a value 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.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/result)
pub fn result( &self ) -> Option< ReaderResult > {
let result = js!( return @{self}.result; );
match result {
Value::Undefined | Value::Null => None,
Value::String( text ) => Some( ReaderResult::Text( text ) ),
_ => unreachable!( "Unexpected result of a FileReader: {:?}", result )
}
}
}