-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implements #175 using JsObject * Add some comments in demo on disposal * Fixed test suite * Fixed test suite * Update version and Release notes
- Loading branch information
Showing
10 changed files
with
159 additions
and
10 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
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,53 @@ | ||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tewr.Blazor.FileReader | ||
{ | ||
#if NET5 | ||
|
||
/// <summary> | ||
/// Represents an object url for a file. | ||
/// </summary> | ||
/// <remarks>https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL</remarks> | ||
public interface IObjectUrl : IAsyncDisposable | ||
{ | ||
/// <summary> | ||
/// Returns the Object Url. | ||
/// </summary> | ||
string Url => ToString(); | ||
} | ||
|
||
internal class ObjectUrl : IObjectUrl | ||
{ | ||
private string objectUrlDomString; | ||
private readonly IJSRuntime jSRuntime; | ||
private readonly IJSObjectReference file; | ||
|
||
internal ObjectUrl(IJSRuntime jSRuntime, IJSObjectReference file) | ||
{ | ||
this.jSRuntime = jSRuntime; | ||
this.file = file; | ||
} | ||
|
||
public async ValueTask InitAsync() | ||
{ | ||
this.objectUrlDomString = await jSRuntime.InvokeAsync<string>("URL.createObjectURL", this.file); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await jSRuntime.InvokeVoidAsync("URL.revokeObjectURL", this.file); | ||
await file.DisposeAsync(); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return this.objectUrlDomString; | ||
} | ||
} | ||
#endif | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/Demo/Blazor.FileReader.Wasm.Demo/Pages/ObjectUrl.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,18 @@ | ||
@page "/ObjectUrl" | ||
@inherits ObjectUrlBase | ||
|
||
<h1>Hello, object url!</h1> | ||
|
||
<br /> | ||
This demo returns an <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL">Object url</a> | ||
and reads some properties from it in a custom js function. This method is only supported for .NET5 and higher. | ||
<br /> | ||
Don't forget to dispose the url if no longer used, as not doing so will create a memoryleak. Disposing calls <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL">revokeObjectURL</a>. | ||
|
||
<br /> | ||
|
||
<input type="file" @ref=inputElement /> | ||
<button @onclick=ReadFile class="btn btn-primary">Read file object url</button> | ||
<br /> | ||
<br /> | ||
<textarea style="max-width: 100%;" cols="50" rows="20">@Output</textarea> |
47 changes: 47 additions & 0 deletions
47
src/Demo/Blazor.FileReader.Wasm.Demo/Pages/ObjectUrl.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,47 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Tewr.Blazor.FileReader; | ||
|
||
namespace Blazor.FileReader.Demo.Common | ||
{ | ||
public partial class ObjectUrlBase : ComponentBase | ||
{ | ||
[Inject] | ||
IFileReaderService FileReaderService { get; set; } | ||
|
||
public ElementReference inputElement; | ||
public string Output { get; set; } | ||
|
||
public async Task ReadFile() | ||
{ | ||
Output = string.Empty; | ||
this.StateHasChanged(); | ||
var files = await FileReaderService.CreateReference(inputElement).EnumerateFilesAsync(); | ||
|
||
if (!files.Any()) | ||
{ | ||
await WriteLine("No file selected"); | ||
} | ||
|
||
foreach (var file in files) | ||
{ | ||
var objectUrl = await file.GetObjectUrlAsync(); | ||
await using (objectUrl) | ||
{ | ||
// Print the object url | ||
await WriteLine($"Object url: {objectUrl}"); | ||
} | ||
|
||
} | ||
} | ||
|
||
public async Task WriteLine(string log) | ||
{ | ||
Output += $"{log}{Environment.NewLine}"; | ||
await InvokeAsync(StateHasChanged); | ||
} | ||
} | ||
} |
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
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