-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
247 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
@page "/" | ||
@using System.Text | ||
@using Blazor.FileReader.Wasm.Demo.Shared | ||
<h1>Hello, world!</h1> | ||
|
||
<IndexCommon BufferSize="81920"/> | ||
Welcome to your new app. | ||
|
||
<div class="container"> | ||
<div class="row mb-2"> | ||
<div class="col"> | ||
<DataInputFileForm @ref="_dataInputFileForm" | ||
FileContent="_fileContent" | ||
SiteID="_id" | ||
OnUseLogs="SaveData" /> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
|
||
@code { | ||
private int _id; | ||
private string _status = ""; | ||
private DataInputFileForm _dataInputFileForm; | ||
|
||
private void SaveData() | ||
{ | ||
//... | ||
} | ||
}"/> |
36 changes: 36 additions & 0 deletions
36
src/Demo/Blazor.FileReader.Wasm.Demo/Shared/DataInputFileForm.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@using Tewr.Blazor.FileReader; | ||
@inject Tewr.Blazor.FileReader.IFileReaderService fileReaderService | ||
<div class="container"> | ||
<div class="row mb-2"> | ||
<div class="col"> | ||
|
||
<div class="input-group"> | ||
<div class="custom-file"> | ||
<input type="file" class="custom-file-input" id="jsonInputFilename" @ref="InputElement" @onchange="FilesSelected" accept=".json" /> | ||
<label class="custom-file-label" for="jsonInputFilename">@Label</label> | ||
</div> | ||
<div class="input-group-append"> | ||
<button class="btn btn-outline-secondary" type="button" @onclick="OnConvertData">Convert text</button> | ||
<button class="btn btn-outline-secondary" type="button" @onclick="OnUseLogs">Use logs</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
@if (LoadedData != null) | ||
{ | ||
<div class="row mb-2"> | ||
<div class="col"> | ||
<div class="text-info">Number of logs loaded: @LoadedData.Count</div> | ||
</div> | ||
</div> | ||
} | ||
<div class="row"> | ||
<div class="col"> | ||
<div class="alert alert-info"> | ||
<textarea class="form-control" readonly>@_status</textarea> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
84 changes: 84 additions & 0 deletions
84
src/Demo/Blazor.FileReader.Wasm.Demo/Shared/DataInputFileForm.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Text.Json; | ||
|
||
namespace Blazor.FileReader.Wasm.Demo.Shared | ||
{ | ||
public partial class DataInputFileForm | ||
{ | ||
|
||
[Parameter] public string Label { get; set; } = "Select file (-s)"; | ||
[Parameter] public string FileContent { get; set; } = ""; | ||
[Parameter] public EventCallback OnUseLogs { get; set; } | ||
[Parameter] public List<SiteLog> LoadedData { get; set; } = new List<SiteLog>(); | ||
[Parameter] public int SiteID { get; set; } | ||
ElementReference InputElement; | ||
private string _status; | ||
|
||
async Task FilesSelected() | ||
{ | ||
LoadedData = new List<SiteLog>(); | ||
_status = "Loading text from file (-s).. " + Environment.NewLine; | ||
foreach (var file in await fileReaderService.CreateReference(InputElement).EnumerateFilesAsync()) | ||
{ | ||
var fileInfo = await file.ReadFileInfoAsync(); | ||
|
||
var filename = fileInfo.Name; | ||
_status += $"Loading {filename}, "; | ||
this.InvokeAsync(this.StateHasChanged); | ||
using (Stream stream = await file.OpenReadAsync()) | ||
{ | ||
this.InvokeAsync(this.StateHasChanged); | ||
FileContent = await ReadAllText(stream, Encoding.UTF8); | ||
} | ||
_status += $"loaded text from file with length {FileContent.Length}, "; | ||
this.InvokeAsync(this.StateHasChanged); | ||
} | ||
} | ||
|
||
public async Task<string> ReadAllText(Stream stream, Encoding encoding) | ||
{ | ||
|
||
using (var reader = new StreamReader(stream, encoding)) | ||
{ | ||
var result = await reader.ReadToEndAsync(); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
private async Task OnConvertData() | ||
{ | ||
if (!string.IsNullOrEmpty(FileContent)) | ||
{ | ||
_status += $"{Environment.NewLine}Parsing text to logs..."; | ||
try | ||
{ | ||
LoadedData.AddRange(await ConvertJsonToSiteLogs(FileContent)); | ||
_status += $"{Environment.NewLine}Parsed data into {LoadedData.Count} site logs. "; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_status += $"{Environment.NewLine}Unable to convert file content:"; | ||
_status += ex.Message; | ||
} | ||
} | ||
else | ||
{ | ||
_status += "There is no text loaded."; | ||
} | ||
this.InvokeAsync(this.StateHasChanged); | ||
} | ||
|
||
|
||
public async Task<List<SiteLog>> ConvertJsonToSiteLogs(string json) | ||
{ | ||
return JsonSerializer.Deserialize<SiteLog[]>(json).ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Blazor.FileReader.Wasm.Demo.Shared | ||
{ | ||
public class Site | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace Blazor.FileReader.Wasm.Demo.Shared | ||
{ | ||
public class SiteLog | ||
{ | ||
public int ID { get; set; } | ||
public DateTime DateTime { get; set; } | ||
public Site Site { get; set; } | ||
public int SiteID { get; set; } | ||
public double[] Lev { get; set; } | ||
} | ||
} |