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

  1. Register the endpoint

    Add on_startup_register_uri_file (on api file upload) with URI uploads, which answers on POST /api/v1/uploads. Set a scope: *loggedin or a custom one. A route without a scope answers 403. Set the filesize pin (the default is 5 MB) and a rate_limit.

  2. Validate before storing

    The handler function gets a file pin with entries of filename, path and type, saved in the entity's temp directory. Check it with file_is_type (is file type) and file_info (file info) into a branch (branch), and reply 400 with httpconnection_set_json (reply in JSON) on the false path.

  3. Name it and move it out of temp

    file_update_name (update name) with a value_guid (generate guid) replaces the client's name and keeps the extension. Then file_move (move) into a private folder such as system_assets/uploads, or file_move_public (move to public) if the file is truly public. Its file pin holds the new path. See public vs private.

  4. Record it and reply

    Create a document in uploads with function_create_document_from_mutations (create document): owner_guid from user_current (current user), the stored path, the original name and status: uploaded. Reply 201 with its guid.

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.

Post a file from a script
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.

Page template
<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.

  1. Load the upload

    web_action (on web action) with event attach-photo. Read upload from its form pin and load the document with function_get_document (get document).

  2. Check the owner

    Compare its owner_guid with user_current through user_get_guid (get guid), using condition_equal (==) into a branch. On false, answer with web_errors (web form errors).

  3. Queue the work

    Set status: queued with function_update_document_mutations (update document), then hand the guid to the worker from step 3 with function_custom_execute_from_queue (execute in queue).

  4. Show progress

    web_update (web update) with target #photo-status and the html of web_template (web template) holding the template below. Give that template a custom pin guid.

Template on web_template: progress
<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.

  1. Pick up the job

    The function you queued starts with trigger_custom_function (create). Set status: processing and read the file with file_from_assets (get file from assets) and the stored path.

  2. Transform it

    Use the file blocks, for example function_resize_image (resize) or function_image_to_webp (convert to webp), and write the result to the document or to a new asset. See Queue.

  3. Mark the outcome

    Set status to done or failed. That is the value the page is polling for.

  4. 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 filesize pin; the asset API rejects oversize files with FILE_TOO_BIG) and validate the type by content, not only by extension.
  • Keep user uploads out of public unless 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.