From ce7eb63fa84cd5746266f64618f79d6240d72212 Mon Sep 17 00:00:00 2001 From: Tor KNUTSSON Date: Sun, 26 Dec 2021 23:04:20 +0100 Subject: [PATCH] Fixes a bug that would throw PlatformNotSupportedException under net6 (#186) --- .../FileReaderJsInterop.InteropFileStream.cs | 5 ++ src/Blazor.FileReader/FileReaderJsInterop.cs | 3 + .../Tewr.Blazor.FileReader.md | 56 +++++++++++++++++++ .../Tewr.Blazor.FileReader.xml | 23 ++++++++ .../Blazor.FileReader.Wasm.Demo/Program.cs | 2 +- 5 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/Blazor.FileReader/FileReaderJsInterop.InteropFileStream.cs b/src/Blazor.FileReader/FileReaderJsInterop.InteropFileStream.cs index 58a3227d..19521aed 100644 --- a/src/Blazor.FileReader/FileReaderJsInterop.InteropFileStream.cs +++ b/src/Blazor.FileReader/FileReaderJsInterop.InteropFileStream.cs @@ -55,6 +55,11 @@ public override void Flush() public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { ThrowIfDisposed(); + if (Position >= length) + { + return 0; + } + var bytesRead = await fileReaderJsInterop.ReadFileAsync(fileRef, buffer, Position, offset, count, cancellationToken); Position += bytesRead; return bytesRead; diff --git a/src/Blazor.FileReader/FileReaderJsInterop.cs b/src/Blazor.FileReader/FileReaderJsInterop.cs index 8c88a86f..50da194d 100644 --- a/src/Blazor.FileReader/FileReaderJsInterop.cs +++ b/src/Blazor.FileReader/FileReaderJsInterop.cs @@ -45,10 +45,13 @@ internal void Initialize() UnmarshalledRuntime = new JSUnmarshalledRuntime(CurrentJSRuntime); } #endif + +#if NET5 || NETSTANDARD20 if (UnmarshalledRuntime is null) { throw new PlatformNotSupportedException($"{nameof(_options.UseWasmSharedBuffer)}=true is not supported on this platform: Unable to acquire {nameof(IJSUnmarshalledRuntime)}."); } +#endif } } diff --git a/src/Blazor.FileReader/Tewr.Blazor.FileReader.md b/src/Blazor.FileReader/Tewr.Blazor.FileReader.md index a8e156f1..df868f66 100644 --- a/src/Blazor.FileReader/Tewr.Blazor.FileReader.md +++ b/src/Blazor.FileReader/Tewr.Blazor.FileReader.md @@ -67,9 +67,13 @@ - [CreateMemoryStreamAsync()](#M-Tewr-Blazor-FileReader-IFileReference-CreateMemoryStreamAsync-System-Threading-CancellationToken- 'Tewr.Blazor.FileReader.IFileReference.CreateMemoryStreamAsync(System.Threading.CancellationToken)') - [CreateMemoryStreamAsync()](#M-Tewr-Blazor-FileReader-IFileReference-CreateMemoryStreamAsync-System-Int32- 'Tewr.Blazor.FileReader.IFileReference.CreateMemoryStreamAsync(System.Int32)') - [CreateMemoryStreamAsync()](#M-Tewr-Blazor-FileReader-IFileReference-CreateMemoryStreamAsync-System-Int32,System-Threading-CancellationToken- 'Tewr.Blazor.FileReader.IFileReference.CreateMemoryStreamAsync(System.Int32,System.Threading.CancellationToken)') + - [GetJSObjectReferenceAsync()](#M-Tewr-Blazor-FileReader-IFileReference-GetJSObjectReferenceAsync 'Tewr.Blazor.FileReader.IFileReference.GetJSObjectReferenceAsync') + - [GetObjectUrlAsync()](#M-Tewr-Blazor-FileReader-IFileReference-GetObjectUrlAsync 'Tewr.Blazor.FileReader.IFileReference.GetObjectUrlAsync') - [OpenReadAsync()](#M-Tewr-Blazor-FileReader-IFileReference-OpenReadAsync 'Tewr.Blazor.FileReader.IFileReference.OpenReadAsync') - [OpenReadBase64Async()](#M-Tewr-Blazor-FileReader-IFileReference-OpenReadBase64Async 'Tewr.Blazor.FileReader.IFileReference.OpenReadBase64Async') - [ReadFileInfoAsync()](#M-Tewr-Blazor-FileReader-IFileReference-ReadFileInfoAsync 'Tewr.Blazor.FileReader.IFileReference.ReadFileInfoAsync') +- [IObjectUrl](#T-Tewr-Blazor-FileReader-IObjectUrl 'Tewr.Blazor.FileReader.IObjectUrl') + - [Url](#P-Tewr-Blazor-FileReader-IObjectUrl-Url 'Tewr.Blazor.FileReader.IObjectUrl.Url') - [PasteEventOptions](#T-Tewr-Blazor-FileReader-DropEvents-PasteEventOptions 'Tewr.Blazor.FileReader.DropEvents.PasteEventOptions') - [Additive](#P-Tewr-Blazor-FileReader-DropEvents-PasteEventOptions-Additive 'Tewr.Blazor.FileReader.DropEvents.PasteEventOptions.Additive') - [PlatformConfig](#T-Tewr-Blazor-FileReader-PlatformConfig 'Tewr.Blazor.FileReader.PlatformConfig') @@ -775,6 +779,36 @@ The length of the resulting [MemoryStream](http://msdn.microsoft.com/query/dev14 A [MemoryStream](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.IO.MemoryStream 'System.IO.MemoryStream') representing the full file, with [Position](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.IO.MemoryStream.Position 'System.IO.MemoryStream.Position') set to 0. +##### Parameters + +This method has no parameters. + + +### GetJSObjectReferenceAsync() `method` + +##### Summary + +Returns the underlying file object as an [IJSObjectReference](#T-Microsoft-JSInterop-IJSObjectReference 'Microsoft.JSInterop.IJSObjectReference') + +##### Returns + + + +##### Parameters + +This method has no parameters. + + +### GetObjectUrlAsync() `method` + +##### Summary + +Returns an object url for a file. + +##### Returns + + + ##### Parameters This method has no parameters. @@ -824,6 +858,28 @@ An object containing the file metadata This method has no parameters. + +## IObjectUrl `type` + +##### Namespace + +Tewr.Blazor.FileReader + +##### Summary + +Represents an object url for a file. + +##### Remarks + +https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL + + +### Url `property` + +##### Summary + +Returns the Object Url. + ## PasteEventOptions `type` diff --git a/src/Blazor.FileReader/Tewr.Blazor.FileReader.xml b/src/Blazor.FileReader/Tewr.Blazor.FileReader.xml index 52873bf1..e86d784f 100644 --- a/src/Blazor.FileReader/Tewr.Blazor.FileReader.xml +++ b/src/Blazor.FileReader/Tewr.Blazor.FileReader.xml @@ -224,6 +224,18 @@ Provides properties and instance methods for the reading file metadata and aids in the creation of Readonly Stream objects. + + + Returns the underlying file object as an + + + + + + Returns an object url for a file. + + + Opens a read-only to read the file. @@ -404,6 +416,17 @@ Should reference either an input element of type file or a drop target. a new instance of + + + Represents an object url for a file. + + https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL + + + + Returns the Object Url. + + Returns true if the application is running on WASM. diff --git a/src/Demo/Net6/Blazor.FileReader.Wasm.Demo/Program.cs b/src/Demo/Net6/Blazor.FileReader.Wasm.Demo/Program.cs index b3594292..def53f9b 100644 --- a/src/Demo/Net6/Blazor.FileReader.Wasm.Demo/Program.cs +++ b/src/Demo/Net6/Blazor.FileReader.Wasm.Demo/Program.cs @@ -8,6 +8,6 @@ builder.RootComponents.Add("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); -builder.Services.AddFileReaderService(); +builder.Services.AddFileReaderService(x => x.UseWasmSharedBuffer = true); await builder.Build().RunAsync();