You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+128-3Lines changed: 128 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,136 @@ A Blazor wrapper for the browser [File API](https://www.w3.org/TR/FileAPI/)
10
10
11
11
The API provides a standard for representing file objects in the browser and ways to select them and access their data. One of the most used interfaces that is at the core of this API is the [Blob](https://www.w3.org/TR/FileAPI/#dfn-Blob) interface. This project implements a wrapper around the API for Blazor so that we can easily and safely interact with files in the browser.
12
12
13
-
**The wrapper is still being developed so the API Coverage is very limited currently.**
14
-
15
13
# Demo
16
14
The sample project can be demoed at https://kristofferstrube.github.io/Blazor.FileAPI/
17
15
18
16
On each page you can find the corresponding code for the example in the top right corner.
19
17
20
-
On the *API Coverage Status* page you can get an overview over what parts of the API we support currently.
18
+
On the *API Coverage Status* page you can get an overview over what parts of the API we support currently.
19
+
20
+
# Getting Started
21
+
## Prerequisites
22
+
You need to install .NET 6.0 or newer to use the library.
The package can be used in Blazor WebAssembly and Blazor Server projects.
34
+
## Import
35
+
You also need to reference the package in order to use it in your pages. This can be done in `_Import.razor` by adding the following.
36
+
```razor
37
+
@using KristofferStrube.Blazor.FileAPI
38
+
```
39
+
40
+
## Creating wrapper instances
41
+
Most of this library is wrapper classes which can be instantiated from your code using the static `Create` and `CreateAsync` methods on the wrapper classes.
42
+
An example could be to create an instance of a `Blob` that contains the text `"Hello World!"` and gets its `Size` and `Type`, read it as a `ReadableStream`, read as text directly, and slice it into a new `Blob` like this.
All creator methods take an `IJSRuntime` instance as the first parameter. The above sample will work in both Blazor Server and Blazor WebAssembly. If we only want to work with Blazor WebAssembly we can use the `InProcess` variant of the wrapper class. This is equivalent to the relationship between `IJSRuntime` and `IJSInProcessRuntime`. We can recreate the above sample using the `BlobInProcess` which will simplify some of the methods we can call on the `Blob` and how we access attributes.
Some of the methods wrap a `Promise` so even in the `InProcess` variant we need to await it like we see for `TextAsync` above.
75
+
76
+
If you have an `IJSObjectReference` or an `IJSInProcessObjectReference` for a type equivalent to one of the classes wrapped in this package then you can construct a wrapper for it using another set of overloads of the static `Create` and `CreateAsync` methods on the appropriate class. In the below example we create wrapper instances from existing JS references to a `File` object.
77
+
```csharp
78
+
// Blazor Server compatible.
79
+
IJSObjectReferencejSFile; // JS Reference from other package or your own JSInterop.
80
+
Filefile=File.Create(JSRuntime, jSFile)
81
+
82
+
// InProcess only supported in Blazor WebAssembly.
83
+
IJSInProcessObjectReferencejSFileInProcess; // JS Reference from other package or your own JSInterop.
You can likewise add the `InProcess` variant of the service (`IURLServiceInProcess`) using the `AddURLServiceInProcess` extension method which is only supported in Blazor WebAssembly projects.
125
+
126
+
# Issues
127
+
Feel free to open issues on the repository if you find any errors with the package or have wishes for features.
128
+
129
+
# Related repositories
130
+
This project uses this package to return a rich `ReadableStream` from the `StreamAsync` method on a `Blob`.
This project is going to be used in this package to return a rich `File` object when getting the `File` from a `FileSystemFileHandle` and when writing a `Blob` to a `FileSystemWritableFileSystem`.
This project uses a combination of the two styles present in the two above packages which both eventually will go more towards the style present in this project.
137
+
138
+
# Related articles
139
+
This repository was build with inspiration and help from the following series of articles:
140
+
141
+
-[Wrapping JavaScript libraries in Blazor WebAssembly/WASM](https://blog.elmah.io/wrapping-javascript-libraries-in-blazor-webassembly-wasm/)
142
+
-[Call anonymous C# functions from JS in Blazor WASM](https://blog.elmah.io/call-anonymous-c-functions-from-js-in-blazor-wasm/)
143
+
-[Using JS Object References in Blazor WASM to wrap JS libraries](https://blog.elmah.io/using-js-object-references-in-blazor-wasm-to-wrap-js-libraries/)
144
+
-[Blazor WASM 404 error and fix for GitHub Pages](https://blog.elmah.io/blazor-wasm-404-error-and-fix-for-github-pages/)
145
+
-[How to fix Blazor WASM base path problems](https://blog.elmah.io/how-to-fix-blazor-wasm-base-path-problems/)
/// <param name="start">The start index of the range. If <see langword="null"/> or negative then <c>0</c> is assumed.</param>
74
85
/// <param name="end">The start index of the range. If <see langword="null"/> or larger than the size of the original <see cref="Blob"/> then the size of the original <see cref="Blob"/> is assumed.</param>
75
86
/// <param name="contentType">An optional MIME type of the new <see cref="Blob"/>. If <see langword="null"/> then the MIME type of the original <see cref="Blob"/> is used.</param>
/// Gets some range of the content of a <see cref="Blob"/> as a new <see cref="Blob"/>.
84
+
/// </summary>
85
+
/// <param name="start">The start index of the range. If <see langword="null"/> or negative then <c>0</c> is assumed.</param>
86
+
/// <param name="end">The start index of the range. If <see langword="null"/> or larger than the size of the original <see cref="Blob"/> then the size of the original <see cref="Blob"/> is assumed.</param>
87
+
/// <param name="contentType">An optional MIME type of the new <see cref="Blob"/>. If <see langword="null"/> then the MIME type of the original <see cref="Blob"/> is used.</param>
88
+
/// <returns>A new <see cref="BlobInProcess"/>.</returns>
0 commit comments