Files#
Upload a file#
with open("invoice.pdf", "rb") as handle:
file_doc = client.upload_file("invoice.pdf", handle.read())
print(file_doc["name"]) # the File document
print(file_doc["file_url"]) # "/private/files/invoice.pdf"
upload_file returns the new File document. Its file_url is the path you
use to download the file later.
Attach it to a document#
with open("signed-contract.pdf", "rb") as handle:
client.upload_file(
"signed-contract.pdf",
handle.read(),
doctype="Customer",
docname="Acme Corporation",
)
The file then appears in the document's attachments sidebar. To fill an
Attach or Attach Image field as well, add fieldname:
client.upload_file(
"logo.png",
png_bytes,
doctype="Customer",
docname="Acme Corporation",
fieldname="image",
is_private=False,
)
| Argument | Default | Meaning |
|---|---|---|
filename |
required | Name to store the file under. No directories. |
content |
required | The file's bytes. |
is_private |
True |
Keep the file behind the site's permissions. |
doctype, docname |
none | Document to attach the file to. |
fieldname |
none | Attach field on that document to fill in. |
folder |
Home |
Folder in the file manager. |
Private by default#
Private files live under /private/files/ and are served only to users who
may read the document they are attached to. Public files live under /files/
and anyone with the URL can download them. frappex-client uploads privately unless
you pass is_private=False.
Website users
Users without desk access (website or portal users) can only upload common document and image types; Frappe rejects others.
Download a file#
data = client.download(file_doc["file_url"])
download returns the whole file as bytes, limited to max_response_bytes
(64 MiB by default). For large files, stream to disk with download_to:
with open("backup.zip", "wb") as target:
written = client.download_to("/private/files/backup.zip", target)
print(f"{written} bytes")
Pass max_bytes to stop a download that grows beyond what you expect:
from frappex_client import ResponseTooLargeError
try:
with open("report.csv", "wb") as target:
client.download_to(url, target, max_bytes=10 * 1024 * 1024)
except ResponseTooLargeError:
print("larger than 10 MiB; stopped")
A download is never retried automatically, because part of it may already be written.
Download paths are checked#
download and download_to accept site-relative paths only, such as
/files/logo.png or /private/files/a.pdf. Absolute URLs, //host/... and
paths with .. are refused. A file_url copied from a document can therefore
never send your credentials to another server.
Find a document's attachments#
Attachments are File documents, so you can list them:
files = client.get_list(
"File",
fields=["name", "file_name", "file_url", "is_private"],
filters={"attached_to_doctype": "Customer", "attached_to_name": "Acme Corporation"},
)
To delete an attachment, delete its File document:
client.delete("File", files[0]["name"])