Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Allow custom styled buttons #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion BlazorInputFile/InputFile.razor
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
@implements IDisposable
@inject IJSRuntime JSRuntime

<input type="file" @ref="inputFileElement" @attributes="UnmatchedParameters" />
@if (IsElementHidden)
{
<input id="@ElementId" type="file" @ref="inputFileElement" @attributes="UnmatchedParameters" style="display:none;"/>
}
else
{
<input type="file" @ref="inputFileElement" @attributes="UnmatchedParameters" />
}

@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 ElementId { get; set; } // this id is used by the js function 'wrapInput' which calls click()
[Parameter] public bool IsElementHidden { get; set; } = false;

ElementReference inputFileElement;
IDisposable thisReference;

Expand Down
4 changes: 4 additions & 0 deletions BlazorInputFile/wwwroot/inputfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
destinationUint8Array.set(sourceUint8Array, destinationOffset);

return bytesToRead;
},

wrapInput: function wrapInput(elementId) {
document.getElementById(elementId).click();
}
};

Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,36 @@
This is a prototype for a file input component that may be added to Blazor in the future.

For installation and usage information, see [this blog post](http://blog.stevensanderson.com/2019/09/13/blazor-inputfile/).

## Using a custom <input> button with MatBlazor:

```csharp
<MatButton OnClick="@(() => OnButtonClick("myInput"))" Label="Choose File"></MatButton>
<InputFile IsElementHidden="true" OnChange="HandleFileSelected" ElementId="myInput"> </InputFile>

@if (file != null)
{
<p>Name: @file.Name</p>
<p>Size in bytes: @file.Size</p>
<p>Last modified date: @file.LastModified.ToShortDateString()</p>
<p>Content type (not always supplied by the browser): @file.Type</p>
}

@code {

public async void OnButtonClick(string elementID)
{
await JSRuntime.InvokeAsync<string>("BlazorInputFile.wrapInput", elementID);
}

IFileListEntry file;

void HandleFileSelected(IFileListEntry[] files)
{
file = files.FirstOrDefault();
}

}
```

Reference from [here](https://stackoverflow.com/a/9546968)