Reference · Files and processes

read_upload

signature
read_upload(map) -> str | error(str)

What it does

The contents of a file the request uploaded, as text: for a CSV, a JSON file or a script the user sends, read and used without saving it. The argument is a file record from request.files ({ field, filename, type, size }) or the file a component passed to a server fn. Bytes that are not UTF-8 are replaced, so a binary file comes out garbled: save those with save_upload.

Examples

A page whose form sends a CSV, and the route that reads it without saving it:

in a program
route GET /import =    <form method="post" action="/import" enctype="multipart/form-data">        <input type="file" name="sheet" accept=".csv,text/csv" required/>        <button>Import</button>    </form>route POST /import {    let rows = read_upload(request.files.sheet)? | split("\n") | filter((l) => l != "")    for r in rows {        let [name, email] = split(r, ",")        db.exec("insert into contacts (name, email) values (?, ?)", name, email)    }    redirect("/contacts")}

Edge cases

let r = read_upload({ field: "sheet", filename: "a.csv", type: "text/csv", size: 10 })print(r)print(is_err(r), r.error)print(read_upload({}) ?? "")
output
err("read_upload: the current request has no files")true read_upload: the current request has no files

Errors

Every failure is an error value, so ? answers the request with it and ?? gives a default; nothing here is a runtime error.

Chaining

in a program
route POST /settings {    let s = request.files.config | read_upload | json.parse    s?}

See also

save_upload, read_file, json.parse, split

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On read_upload(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo