Reference · Files and processes
read_upload
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:
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
- Outside a request, or in a request that uploaded nothing, the result is an error
value:
read_upload: the current request has no files. A record that is not one of this request's files is an error value too. - The text is the whole file in memory; the body's size is limited by the
quillo runoption--max-body(10 MiB by default):quillo run main.qwe --max-body 50MBtakes larger uploads. See the options. - The file stays with the request:
read_uploadreads it as often as asked.
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({}) ?? "")err("read_upload: the current request has no files")true read_upload: the current request has no filesErrors
Every failure is an error value, so ? answers the request with it and ?? gives a
default; nothing here is a runtime error.
Chaining
route POST /settings { let s = request.files.config | read_upload | json.parse s?}See also
save_upload, read_file, json.parse, split
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