eQuantic.UIeQuantic.UI
Docs
Playground
GitHub
DocsServer
Uploads
Edit this page
5 min read
Files a person sends to the app's own server: a 3D model to print, a PDF to attach, a spreadsheet to import. The app declares what it accepts once, in C# that the page and the server both read, drops a FileUpload on the page, and receives each file in a Server Action as an UploadedFile it reads as a stream. Everything between, the parts, the progress, a connection that drops, a page that is reloaded halfway, is the SDK's.
Since 0.2.0-preview.66
What the app accepts
1
2
3
4
5
6
public static class Uploads
{
public static readonly UploadPolicy Models = new("models",
filters: [new FileFilter("3D models", "stl", "obj", "3mf")],
maxSize: 250 * 1024 * 1024);
}
A policy has a name the server knows it by, the formats it accepts (an empty list accepts any file) and the largest file it takes. requireSignedIn: true keeps it for signed-in people.
The server accepts the policies the app names in Program.cs, and MapUI() maps the endpoint the page sends to:
1
builder.Services.AddUI(ui => ui.UseUploads(uploads => uploads.Accept(Uploads.Models)));
The policy is the rule on both sides. The page asks policy.RefusalOf(name, size) before it sends a byte, and the server asks the same rule again, in the same words, whatever the page let through: "Only .stl, .obj, .3mf files are accepted here.", "The file is larger than the 262144000 bytes accepted here." A refusal from the server (a policy it does not accept, a visitor under a policy that requires a signed-in person, too many unfinished uploads) reaches the page as its short English reason.
A page that takes files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[Page("/models")]
public sealed class ModelsScreen : StatefulComponent
{
private string _saved = "";
[ServerAction]
public async Task<string> SaveModel(UploadedFile model)
{
await using var content = await model.OpenRead();
// … store it where the app keeps its data …
await model.Delete();
return model.Name;
}
public override VisualNode Build(ComponentContext context) =>
Column(gap: Space.S4, children: [
FileUpload(Uploads.Models, onCompleted: model => _ = Save(model)),
Text(_saved, TypeRole.BodyM),
]);
private async Task Save(UploadedFile model)
{
var name = await SaveModel(model);
SetState(() => _saved = $"Saved {name}");
}
}
FileUpload draws an area files are dropped on, a button that asks the person for them (the way in for a keyboard and a screen reader), and a row for each file with its progress, its state, Pause, Resume, Cancel and, when it failed, why. What arrives or fails is announced. Its words are the SDK's, in every culture the SDK ships; label: and hint: replace the invitation and the line that says what the area accepts. multiple: false takes one file at a time.
onCompleted receives each file once its last byte arrived. The page hands it to its own Server Action, and the server binds the parameter from the upload's id alone: the name, type, size and policy are the ones it recorded when the upload started, whatever the page sent beside the id. It binds the file only for the person who uploaded it, and only once it is whole.
OpenRead streams the content from where the server keeps it, and Delete removes it. Both are the server's ([ServerOnly]): client code that calls either is refused at the build (EQ2010).
How a file travels
•
In parts of 8 MB, each answered before the next is sent, so the progress moves inside a part and no request comes near the 30 MB a server takes in one by default.
•
A part that fails is sent again, after a pause that grows (0, 1, 3, 5 and 10 seconds), from the offset the server says it holds. Past the last pause the upload fails with "The connection to the server was lost."
•
A reloaded page goes on. The upload's address is remembered in the browser under the file's name, size, date and policy: choosing the same file again asks the server how much of it arrived and sends the rest. A file whose area leaves the screen pauses, and goes on the same way.
•
Cancel removes it, on the server too.
The protocol is tus 1.0, spoken by the browser's runtime and by the server; no page names it.
Where the server keeps them
1
2
3
4
5
6
7
8
9
{
"EQuantic": {
"Uploads": {
"Folder": "uploads",
"Expiry": "1.00:00:00",
"MaxUnfinished": 5
}
}
}
The folder is a staging area: every upload, finished or not, is removed when its expiry passes, so the Server Action stores what it received where the app keeps its data. Without Folder the server uses a folder of its own under the system's temporary folder, and logs which.
An upload belongs to whoever started it: the signed-in person, or the browser, by an HttpOnly cookie (eq-uploads). Nobody else continues it, reads its offset, deletes it or receives it in an action. A page of another site is refused, by the same rule and the same EQuantic:ServerActions:AllowedOrigins as the Server Actions.
Drawing your own
FileUpload is drawn over the IUploads capability, which a page takes to draw something else: one picture as an avatar, a progress in a toolbar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public sealed class ModelButton(IFilePicker? picker, IUploads? uploads) : StatefulComponent
{
private Upload? _upload;
private IDisposable? _changes;
protected override void OnUnmount() => _changes?.Dispose();
public override VisualNode Build(ComponentContext context) => _upload switch
{
null => Button("Send a model", onPressed: () => _ = Choose()),
{ State: UploadState.Failed } failed => Text(failed.Error ?? "", TypeRole.BodyM),
{ State: UploadState.Completed } => Text("Sent", TypeRole.BodyM),
var sending => ProgressBar((float)sending.Sent / sending.Size, label: "Sending the model"),
};
private async Task Choose()
{
if (picker is null || uploads is null) return;
if (await picker.PickFileAsync(Uploads.Models.Filters) is not { } file) return;
var upload = uploads.Start(file, Uploads.Models);
_changes?.Dispose();
_changes = upload.Subscribe(_ => SetState(() => { }));
SetState(() => _upload = upload);
}
}
Start answers at once with the Upload, which goes on by itself. Its State is Waiting, Sending, Paused, Completed, Failed or Cancelled; Sent and Size are its progress in bytes, Error why it failed, and Result the UploadedFile once it completed. Subscribe hears every change and answers an IDisposable, as INetworkStatus does, so a component ends it where it ends.
Since 0.2.0-preview.66
Where it is absent. Server rendering has no browser to send from, and the native shells have no transport yet: IsAvailable is false there, FileUpload draws the same area disabled, and Start answers an upload that has already failed, saying so. The browser enables the area when the page runs there.
Limits
•
One server instance: the records of the uploads live in its memory and beside the bytes, so a balancer that sends the parts of one upload to two instances needs a shared store first.
•
A page on another domain also needs the app's CORS policy to expose the tus headers, which the SDK does not configure.