File Upload & Processing
By the end a visitor can pick a file on a web page, your upload endpoint stores it, and a queue worker processes it in the background while the page shows progress. Machines post to the same endpoint.
1. An upload endpoint takes the file
Register the endpoint
Add
on_startup_register_uri_file(on api file upload) with URIuploads, which answers onPOST /api/v1/uploads. Set a scope:*loggedinor a custom one. A route without a scope answers 403. Set thefilesizepin (the default is 5 MB) and arate_limit.Validate before storing
The handler function gets a
filepin with entries offilename,pathandtype, saved in the entity's temp directory. Check it withfile_is_type(is file type) andfile_info(file info) into abranch(branch), and reply 400 withhttpconnection_set_json(reply in JSON) on the false path.Name it and move it out of temp
file_update_name(update name) with avalue_guid(generate guid) replaces the client's name and keeps the extension. Thenfile_move(move) into a private folder such assystem_assets/uploads, orfile_move_public(move to public) if the file is truly public. Itsfilepin holds the new path. See public vs private.Record it and reply
Create a document in
uploadswithfunction_create_document_from_mutations(create document):owner_guidfromuser_current(current user), the stored path, the original name andstatus: uploaded. Reply 201 with itsguid.
Other systems use the same endpoint. It also accepts a resumable tus 1.0.0 upload, and your function runs the same way once the last byte is in.
curl -X POST "https://<cluster-url>/api/v1/uploads" \
-H "Authorization: Bearer <access_token>" \
-F "file=@report.csv"Staff tools that only need a file on the cluster can skip the endpoint and use the built-in asset API, POST /api/v1/_system/assets/stream, with a token that holds assets_create. See Assets.
2. The page posts the file, then tells the action
The page holds a file input and a form with a hidden field. A short script posts the file to the endpoint, puts the returned guid in that field and submits the form, which runs an ordinary web action.
<main>
<h1>Profile photo</h1>
<input id="photo-file" type="file" accept="image/png,image/jpeg,image/webp">
<form id="photo-form" data-action="attach-photo">
<input type="hidden" name="upload">
<div data-error-for="_form"></div>
</form>
<div id="photo-status"></div>
</main>
<script nonce="{{nonce}}">
document.getElementById('photo-file').addEventListener('change', async function () {
var form = document.getElementById('photo-form');
var body = new FormData();
body.append('file', this.files[0]);
var reply = await fetch('/api/v1/uploads', { method: 'POST', body: body });
if (!reply.ok) {
form.querySelector('[data-error-for="_form"]').textContent = 'That file could not be uploaded.';
return;
}
var upload = await reply.json();
form.elements.upload.value = upload.guid;
form.requestSubmit();
});
</script>Then wire the action. The guid comes from the browser, so check it belongs to the caller before you trust it.
Load the upload
web_action(on web action) witheventattach-photo. Readuploadfrom itsformpin and load the document withfunction_get_document(get document).Check the owner
Compare its
owner_guidwithuser_currentthroughuser_get_guid(get guid), usingcondition_equal(==) into abranch. On false, answer withweb_errors(web form errors).Queue the work
Set
status: queuedwithfunction_update_document_mutations(update document), then hand the guid to the worker from step 3 withfunction_custom_execute_from_queue(execute in queue).Show progress
web_update(web update) withtarget#photo-statusand thehtmlofweb_template(web template) holding the template below. Give that template a custom pinguid.
<div data-action="photo-status" data-param-upload="{{guid}}" data-trigger="every 3s">
Processing your photo
</div>A second action named photo-status reads upload, repeats the owner check and looks at status. Still busy: answer nothing, and the page stays as it is. Done: web_update on #photo-status with the result, which also ends the polling because the element carrying the trigger is gone.
3. The slow part runs in the background
Resizing, parsing, importing and converting never run inside the request.
Pick up the job
The function you queued starts with
trigger_custom_function(create). Setstatus: processingand read the file withfile_from_assets(get file from assets) and the stored path.Transform it
Use the file blocks, for example
function_resize_image(resize) orfunction_image_to_webp(convert to webp), and write the result to the document or to a new asset. See Queue.Mark the outcome
Set
statustodoneorfailed. That is the value the page is polling for.Log failures
Send them to the console and to a storage, with the error-pins pattern from Block Execution.
The safety rules
- Never trust the client's filename for the stored path. Generate your own.
- Cap the file size on the endpoint (the
filesizepin; the asset API rejects oversize files withFILE_TOO_BIG) and validate the type by content, not only by extension. - Keep user uploads out of
publicunless they are truly public. Use private folders and scoped serving, per Serving Protected Files. - Make import pipelines idempotent: re-running the same file updates the same output document, with an upsert on the source guid.
Next steps
Assets Asset paths, APIs, and access rules in depth. Queue Background processing for the heavy lifting. Actions How buttons, forms and polling elements call your flow. Handling City Filter Uploads An upload endpoint that parses and filters rows.Frequently asked
How do I accept file uploads in RUAL?
Through an endpoint registered with on_startup_register_uri_file, whose handler receives the file on its file pin. A web page posts to it by script and passes the result to a web action, because web actions refuse multipart bodies. Record the asset guid or path on a document so you can find it later.
How do I process uploaded files in RUAL without blocking the request?
The upload flow stores the file and enqueues a processing job with function_custom_execute_from_queue. A queue worker flow reads the file with file_from_assets, transforms it, and writes results back to a document or a new asset, marking progress on a job document so users can poll the state.
What safety rules apply to RUAL file uploads?
Never trust the client's filename for the stored path, validate the type by content and not by extension, keep user uploads out of public storage unless they are truly public, and make import pipelines idempotent by upserting on the source guid.