|
1 | 1 | using Microsoft.JSInterop; |
| 2 | +using System.Text.Json.Serialization; |
2 | 3 |
|
3 | 4 | namespace KristofferStrube.Blazor.FileAPI; |
4 | 5 |
|
@@ -27,15 +28,21 @@ public static async Task<FileReader> CreateAsync(IJSRuntime jSRuntime) |
27 | 28 | { |
28 | 29 | IJSObjectReference helper = await jSRuntime.GetHelperAsync(); |
29 | 30 | IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructFileReader"); |
30 | | - return new FileReader(jSRuntime, jSInstance); |
| 31 | + var fileReader = new FileReader(jSRuntime, jSInstance); |
| 32 | + await helper.InvokeVoidAsync("registerEventHandlers", fileReader, jSInstance); |
| 33 | + return fileReader; |
31 | 34 | } |
32 | 35 |
|
33 | 36 | /// <summary> |
34 | 37 | /// Constructs a wrapper instance for a given JS Instance of a <see cref="FileReader"/>. |
35 | 38 | /// </summary> |
36 | 39 | /// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param> |
37 | 40 | /// <param name="jSReference">A JS reference to an existing <see cref="FileReader"/>.</param> |
38 | | - internal FileReader(IJSRuntime jSRuntime, IJSObjectReference jSReference) : base(jSRuntime, jSReference) { } |
| 41 | + internal FileReader(IJSRuntime jSRuntime, IJSObjectReference jSReference) : base(jSRuntime, jSReference) { |
| 42 | + ObjRef = DotNetObjectReference.Create(this); |
| 43 | + } |
| 44 | + |
| 45 | + public DotNetObjectReference<FileReader> ObjRef { get; init; } |
39 | 46 |
|
40 | 47 | public async Task ReadAsArrayBufferAsync(Blob blob) |
41 | 48 | { |
@@ -69,6 +76,102 @@ public async Task AbortAsync(Blob blob) |
69 | 76 | public async Task<ushort> GetReadyStateAsync() |
70 | 77 | { |
71 | 78 | IJSObjectReference helper = await helperTask.Value; |
72 | | - return await helper.InvokeAsync<ushort>("readyState"); |
| 79 | + return await helper.InvokeAsync<ushort>("getAttribute", JSReference, "readyState"); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Checks whether the result is a either a string or a byte array. |
| 84 | + /// </summary> |
| 85 | + /// <returns>Either the type of <see langword="string"/> or type of <see cref="byte"/>[].</returns> |
| 86 | + public async Task<Type?> GetResultTypeAsync() |
| 87 | + { |
| 88 | + IJSObjectReference helper = await helperTask.Value; |
| 89 | + var isArrayBuffer = await helper.InvokeAsync<bool>("isArrayBuffer", JSReference); |
| 90 | + return isArrayBuffer ? typeof(byte[]) : typeof(string); |
| 91 | + } |
| 92 | + |
| 93 | + public async Task<string?> GetResultAsStringAsync() |
| 94 | + { |
| 95 | + IJSObjectReference helper = await helperTask.Value; |
| 96 | + return await helper.InvokeAsync<string>("getAttribute", JSReference, "result"); |
| 97 | + } |
| 98 | + |
| 99 | + public async Task<byte[]?> GetResultAsByteArrayAsync() |
| 100 | + { |
| 101 | + IJSObjectReference helper = await helperTask.Value; |
| 102 | + var jSResult = await helper.InvokeAsync<IJSObjectReference>("getAttribute", JSReference, "result"); |
| 103 | + return await helper.InvokeAsync<byte[]>("arrayBuffer", jSResult); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Gets the error object reference which will be null if no error occured. |
| 108 | + /// </summary> |
| 109 | + /// <returns>A nullable IJSObjectReference because it was out of scope to wrap the Exception API.</returns> |
| 110 | + public async Task<IJSObjectReference?> GetErrorAsync() |
| 111 | + { |
| 112 | + IJSObjectReference helper = await helperTask.Value; |
| 113 | + return await helper.InvokeAsync<IJSObjectReference?>("getAttribute", JSReference, "error"); |
| 114 | + } |
| 115 | + |
| 116 | + [JsonIgnore] |
| 117 | + public Func<ProgressEvent, Task>? OnLoadStart { get; set; } |
| 118 | + |
| 119 | + [JsonIgnore] |
| 120 | + public Func<ProgressEvent, Task>? OnProgress { get; set; } |
| 121 | + |
| 122 | + [JsonIgnore] |
| 123 | + public Func<ProgressEvent, Task>? OnLoad { get; set; } |
| 124 | + |
| 125 | + [JsonIgnore] |
| 126 | + public Func<ProgressEvent, Task>? OnAbort { get; set; } |
| 127 | + |
| 128 | + [JsonIgnore] |
| 129 | + public Func<ProgressEvent, Task>? OnError { get; set; } |
| 130 | + |
| 131 | + [JsonIgnore] |
| 132 | + public Func<ProgressEvent, Task>? OnLoadEnd { get; set; } |
| 133 | + |
| 134 | + [JSInvokable] |
| 135 | + public async Task InvokeOnLoadStart(IJSObjectReference jsProgressEvent) |
| 136 | + { |
| 137 | + if (OnLoadStart is null) return; |
| 138 | + await OnLoadStart.Invoke(new ProgressEvent(jSRuntime, jsProgressEvent)); |
| 139 | + } |
| 140 | + |
| 141 | + [JSInvokable] |
| 142 | + public async Task InvokeOnProgress(IJSObjectReference jsProgressEvent) |
| 143 | + { |
| 144 | + if (OnProgress is null) return; |
| 145 | + await OnProgress.Invoke(new ProgressEvent(jSRuntime, jsProgressEvent)); |
| 146 | + } |
| 147 | + |
| 148 | + [JSInvokable] |
| 149 | + public async Task InvokeOnLoad(IJSObjectReference jsProgressEvent) |
| 150 | + { |
| 151 | + if (OnLoad is null) return; |
| 152 | + await OnLoad.Invoke(new ProgressEvent(jSRuntime, jsProgressEvent)); |
| 153 | + } |
| 154 | + |
| 155 | + [JSInvokable] |
| 156 | + public async Task InvokeOnAbort(IJSObjectReference jsProgressEvent) |
| 157 | + { |
| 158 | + if (OnAbort is null) return; |
| 159 | + await OnAbort.Invoke(new ProgressEvent(jSRuntime, jsProgressEvent)); |
| 160 | + } |
| 161 | + |
| 162 | + [JSInvokable] |
| 163 | + public async Task InvokeOnError(IJSObjectReference jsProgressEvent) |
| 164 | + { |
| 165 | + if (OnError is null) return; |
| 166 | + await OnError.Invoke(new ProgressEvent(jSRuntime, jsProgressEvent)); |
| 167 | + } |
| 168 | + |
| 169 | + [JSInvokable] |
| 170 | + public async Task InvokeOnLoadEnd(IJSObjectReference jsProgressEvent) |
| 171 | + { |
| 172 | + if (OnLoadEnd is null) return; |
| 173 | + await OnLoadEnd.Invoke(new ProgressEvent(jSRuntime, jsProgressEvent)); |
73 | 174 | } |
| 175 | + |
| 176 | + |
74 | 177 | } |
0 commit comments