Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: SpawnJSObject, IEnumerable<File>
Source: JSObjects/FileList.cs
MDN Reference: FileList on MDN
The FileList interface represents an object of this type returned by the files property of the HTML input element; this lets you access the list of files selected with the input type="file" element. It's also used for a list of files dropped into web content when using the drag and drop API; see the DataTransfer object for details on this usage. https://developer.mozilla.org/en-US/docs/Web/API/FileList
| Signature | Description |
|---|---|
FileList(SpawnJSObjectReference _ref) |
Deserialization constructor |
| Property | Type | Access | Description |
|---|---|---|---|
Length |
int |
get | A read-only value indicating the number of files in the list. |
| Method | Return Type | Description |
|---|---|---|
GetEnumerator() |
IEnumerator<File> |
|
Item(int index) |
File |
Returns a File object representing the file at the specified index in the file list. |
FirstOrDefault() |
File? |
Returns first or default |
LastOrDefault() |
File? |
Returns last or default |
ToArray() |
File[] |
Returns the array as a .Net Array |
ToArray(int start, int count) |
File[] |
Returns the array as a .Net Array |
ToList() |
List<File> |
Returns the array as a .Net List |
ToList(int start, int count) |
List<File> |
Returns the array as a .Net List |
SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:
- Properties and methods use PascalCase (e.g.,
readyStatebecomesReadyState)- Events use ActionEvent with
+=/-=(e.g.,addEventListener("click", fn)becomesOnClick += handler)- Async methods return
Task<T>instead ofPromise<T>- Objects should be disposed with
usingstatements- Access via
JS.Get<FileList>(...)or constructornew FileList(...)
const output = document.querySelector(".output");
const fileInput = document.querySelector("#myfiles");
fileInput.addEventListener("change", () => {
for (const file of fileInput.files) {
output.innerText += `\n${file.name}`;
}
});In this example, we log the names of all the files selected by the user.
<input id="myfiles" multiple type="file" />
<pre class="output">Selected files:</pre>.output {
overflow: scroll;
margin: 1rem 0;
height: 200px;
}JavaScript (MDN):
const output = document.querySelector(".output");
const fileInput = document.querySelector("#myfiles");
fileInput.addEventListener("change", () => {
for (const file of fileInput.files) {
output.innerText += `\n${file.name}`;
}
});C# (SpawnDev.SpawnJS):
// Requires: builder.Services.AddSpawnJSRuntime();
// Inject SpawnJSRuntime in your component or service:
// [Inject] SpawnJSRuntime JS { get; set; }
using var output = JS.Get<Document>("document").QuerySelector<HTMLElement>(".output");
using var fileInput = JS.Get<Document>("document").QuerySelector<HTMLElement>("#myfiles");
fileInput.addEventListener("change", () => {
for (const file of fileInput.files) {
output.innerText += $"\n{file.name}";
}
});