FileUpload
Lets a person choose files and uploads them to a server — each one right after it is chosen, or all together when the form is submitted. It is
a wrapper, not a button
: what opens the file picker is up to the markup. The element provides a ready function
pick:@pickFiles
for any button and publishes the state of every file as data the markup can show however it likes.
Type:
wrapper
FileUpload url:"https://api.example.com/v1/attachments" files:@files pick:@pickFiles
Column spacing:8
OutlinedButton onPressed:(){pickFiles()}
Text "Attach a file"
Text $"Files: ${length(files)}"
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url
|
string |
""
|
Where each file is sent |
accept
|
string |
""
|
Which files the picker offers, as in
<input type=file>
:
"image/*,.pdf"
|
multiple
|
bool | false | Several files can be chosen at once |
maxBytes
|
int | 0 |
Maximum file size in bytes; 0 — no limit. A larger file is not sent and gets the
error
status right away
|
idField
|
string |
id
|
The key of the identifier in the server's JSON response |
autoUpload
|
bool | true |
Upload each file right after it is chosen.
false
— wait for a call to
uploadFiles()
|
onUploaded
|
callback | — |
Called when, after
uploadFiles()
, all files have the
done
status
|
The outputs are
@
variables the element declares for its subtree:
| Output | What it holds |
|---|---|
files:@files
|
The list of files in the order they were chosen; each is
{name, size, type, preview, status, id}
.
status
:
ready
/
uploading
/
done
/
error
;
id
is empty until
done
|
ids:@ids
|
The identifiers of files with the
done
status, in the same order — what the form sends along with the message
|
pending:@busy
|
true
while at least one file is in flight — a condition for the submit button
|
pick:@pickFiles
|
A function: opens the system file picker (
accept
,
multiple
)
|
remove:@removeFile
|
A function:
removeFile(index: i)
removes a file from
files
and
ids
. The argument is named
|
upload:@uploadFiles
|
A function: sends all
ready
files and retries all
error
ones (except files larger than
maxBytes
)
|
The variable names are chosen by the markup:
files:@attachments
declares
attachments
.
How a file is sent
Each file is sent separately with a
POST url
request; the body is
the bytes of the file
(not multipart). Headers are taken from
HttpHeaders
higher up the tree, except
Content-Type
— that is the file's own type (
application/octet-stream
if the type is unknown);
X-File-Name
is added with the URL-encoded file name. The response is JSON;
idField
names the key with the identifier of the uploaded file.
A file gets the
error
status when it is larger than
maxBytes
, rejected by the server (any non-2xx response), failed on the network, or accepted with a 2xx but
without an identifier
in the response: a file that cannot be referred to does not count as uploaded.
Examples
Attachment list with removal
HttpHeaders Authorization:"Bearer token"
FileUpload url:"https://api.example.com/v1/attachments" accept:"image/*,.pdf" multiple:true maxBytes:26214400 files:@files ids:@ids pending:@busy pick:@pickFiles remove:@removeFile
Column
OutlinedButton onPressed:(){pickFiles()}
Text "Attach files"
ListView.builder itemCount:{length(files)} shrinkWrap:true
@Macros id:itemBuilder index:0
ListTile
Text id:title {files[index]['name']}
Text id:subtitle {files[index]['status']}
IconButton id:trailing tooltip:"Remove" onPressed:(){removeFile(index: index)}
Icon close
Text {busy ? "Uploading…" : ""}
Image preview before sending
preview
is an address that
Image src:
understands, only for
image/*
files (for other files and for images larger than
maxBytes
it is empty). On the site it is the file itself; in the app — a copy no larger than 1024 pixels on the long side, ready a moment after the file is chosen.
FileUpload url:"https://api.example.com/v1/attachments" multiple:true accept:"image/*" files:@files pick:@pickFiles
Column spacing:8
OutlinedButton onPressed:(){pickFiles()}
Text "Add photos"
ListView.builder itemCount:{length(files)} shrinkWrap:true
@Macros id:itemBuilder index:0
Image src:{files[index]['preview']} width:56 height:56 fit:cover
Uploading on submit:
autoUpload:false
Chosen files wait with the
ready
status until
uploadFiles()
is called — usually by the form's submit button. When the batch is finished and
all
files are
done
,
onUploaded
is called — after
ids
has been updated. If at least one file failed,
onUploaded
is not called: the person removes the file or presses again. If there is nothing to upload,
onUploaded
is called right away.
A variable is visible to the
descendants
of whoever declared it, not to an ancestor's parameters:
onUploaded
cannot call the function of an
HttpData
placed inside
FileUpload
. So the request goes outside, and the identifiers are passed to it through a page variable:
State @message:"" @attachments:"[]"
HttpData url:"https://api.example.com/v1/tickets" method:"POST" immediate:false refresh:@sendTicket body:{jsonEncode(mapOf("message", message, "attachments", jsonDecode(attachments)))}
FileUpload url:"https://api.example.com/v1/attachments" multiple:true autoUpload:false files:@files ids:@ids pending:@uploading pick:@pickFiles upload:@uploadFiles onUploaded:(){attachments = jsonEncode(ids); sendTicket()}
Column spacing:8
TextField value:$message
OutlinedButton onPressed:(){pickFiles()}
Text "Attach"
FilledButton onPressed:(){uploadFiles()}
Text {uploading ? "Sending…" : "Send"}
Differences between the app and the site
-
Site:
the picker is the browser's
<input type=file>.pickFiles()must be called from a tap: the browser opens the picker only inside a user gesture.previewis the address of the file itself (released onremoveFile). -
App:
the file picker exists in the editor's live preview; a built app does not have it yet, and
pickFiles()does nothing there. - Sending, headers, statuses and outputs are the same.
See also
- Form — the form the attachments are sent with
-
Image
— showing
preview - ListView — the list of files
- Material buttons — a button that opens the picker