forked from DataJuggler/BlazorFileUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputFile.razor
58 lines (48 loc) · 1.88 KB
/
InputFile.razor
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
@implements IDisposable
@inject IJSRuntime JSRuntime
@if (Visible)
{
<input type="file" class="@InputFileClassName" @ref="inputFileElement" @attributes="UnmatchedParameters" />
}
else
{
<input type="file" class="@InputFileClassName" @ref="inputFileElement" @attributes="UnmatchedParameters" hidden />
}
@code {
[Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> UnmatchedParameters { get; set; }
[Parameter] public EventCallback<IFileListEntry[]> OnChange { get; set; }
[Parameter] public int MaxMessageSize { get; set; } = 20 * 1024; // TODO: Use SignalR default
[Parameter] public int MaxBufferSize { get; set; } = 1024 * 1024;
[Parameter] public string InputFileClassName { get; set; }
[Parameter] public bool Visible { get; set; } = true;
ElementReference inputFileElement;
IDisposable thisReference;
[JSInvokable]
public Task NotifyChange(FileListEntryImpl[] files)
{
foreach (var file in files)
{
// So that method invocations on the file can be dispatched back here
file.Owner = (InputFile)(object)this;
}
return OnChange.InvokeAsync(files);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
thisReference = DotNetObjectReference.Create(this);
await JSRuntime.InvokeAsync<object>("BlazorInputFile.init", inputFileElement, thisReference);
}
}
internal Stream OpenFileStream(FileListEntryImpl file)
{
return SharedMemoryFileListEntryStream.IsSupported(JSRuntime)
? (Stream)new SharedMemoryFileListEntryStream(JSRuntime, inputFileElement, file)
: new RemoteFileListEntryStream(JSRuntime, inputFileElement, file, MaxMessageSize, MaxBufferSize);
}
void IDisposable.Dispose()
{
thisReference?.Dispose();
}
}