Files
Upload one file
/a/File_upload
Store one uploaded/data-URI file, create its tenant file record, and update supported parent resources’ legacy file-ID lists.
When to use this endpoint
Use File_upload for CRM1’s sequential upload control or API clients sending a data URI. For true multipart upload, Files_upload is the simpler current route.
Authentication and permissions
Normal API transport requires signed API-key fields or a logged-in CRM session and a tenant context. Session CSRF applies except for the dispatcher’s upload/download exceptions.
These handlers do not add general Files Add/Edit/Delete or file-owner role checks. UI visibility is not that protection. Configured team-scope entry checks are separate and may validate the linked record and its Edit permission before a handler runs; see the route-specific note below. This does not introduce CRM2 role requirements for ordinary CRM1 requests.
When department/team or Assigned Jobs Only scope is configured for the actor, the dispatcher adds record-scope checks. This is selected by account features/actor flags, separately from the CRM2-only Files View check; the existing TeamAccess entry guard has no CRM-version condition. It requires a supported type_enum and visible type_id: 0 Customer, 2 Asset, 3 Job, 4 User, 13 Task or 14 Contract. Upload/deleteAll additionally require the owning object’s Edit grant.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
type_id | Integer-compatible | Yes | Linked resource ID. Presence is checked, but zero is accepted. This is the linked object ID, never the file ID. |
type_enum | Integer-compatible | Effectively yes | Linked resource type; absent becomes zero. Common verified values are 0 Customer, 1 Asset Type, 2 Asset, 3 Job, 4 User, 5 recurring Job, 6 User photo, 7 report-design image, 8 Job form photo, 9 report PDF background, 10 private/non-downloadable, 11 form file, 12 Quote, and 13 Task file. Other modules use later values; use the enum already emitted by the relevant CRM workflow. |
Filedata | Multipart file or data-URI string | Yes | Multipart field when $_FILES is populated, otherwise a data URI. The data-URI branch base64-decodes everything after the first comma without strict validation. |
filename | String | Data-URI branch | Stored display filename. Multipart upload uses the client filename. |
unique | Integer-compatible | No | Truthy makes the successful new upload the only live file for the same type/resource by deleting earlier live files through File_delete. |
Example request
$result = FM_api('File_upload', [
'type_enum' => '2',
'type_id' => '1042',
'filename' => 'asset-photo.jpg',
'Filedata' => 'data:image/jpeg;base64,/9j/4AAQSkZJRg...',
]);
Example response
{
"ok": 1,
"id": 4821,
"url": "files/2/1042/7d4f..."
}
Errors and unusual responses
| Response | Condition |
|---|---|
{"error":"not logged in"} | No authenticated tenant context is available. Invalid signed credentials can be rejected by the dispatcher before the handler runs. |
{"error":"no file uploaded"} | Multipart data exists but its Filedata request value is missing/falsy under the legacy check. |
{"error":"missing or invalid parameter: type_id"} | The type-ID key is absent. |
{"error":"missing parameter: Filedata"} | Neither multipart nor data-URI content is supplied. |
{"error":"file too large. max file size is 12582912 bytes"} | Decoded content exceeds the storage limit. |
Legacy application errors may be returned with HTTP 200. Check the decoded response for error, not only the HTTP status.
Behaviour and edge cases
- The storage URL contains the type, resource ID, and a random MD5 token. The library stores the bytes and inserts metadata containing the uploading user ID.
- Customer, Asset, Job, User, and recurring-Job types update parent file lists/logs inside the storage helper. Customer/Asset lists are then rebuilt again by this endpoint. Job outsourcing propagation may update the linked tenant.
- With
unique=1, previous rows remain as soft-deleted metadata with content removed and deletion-log entries. Filename/content MIME is not validated here; only byte length is enforced. - No pagination applies to this write. Use a fresh metadata/list read before dependent work; an error after storage, audit or notification work does not imply that earlier steps were rolled back.
- The storage URL is files/
/ / . The returned id is the separate new file-record ID, url is tenant-relative, and filename remains the supplied display name. None of those values is the parent resource ID returned in the URL’s third segment. - The configured FILESLIB_MAXSIZE limits decoded bytes (default 12 MiB); PHP/webserver request limits may reject the request earlier. Base64 and multipart transport overhead are separate from this stored-byte limit. The route has no idempotency token; repeating a successful request normally creates another file.
Side effects
- Stores bytes and metadata, stamps creation/edit/access fields, invalidates files cache and refreshes supported parent file lists/User history through the shared storage library. Job outsourcing can propagate linked file identifiers. unique cleanup runs after storing the replacement and is not part of a rollback-safe batch.
See it in Fieldmotion
CRM1
Use Upload Files on Customer, Job, Asset, User, Task, recurring-Job, Asset Type, or generic file widgets.
CRM2
Most modern drop zones use Files_upload; some shared support/client code still targets this singular route.
Related endpoints
Files_uploadaccepts multipart input.File_getMaxUploadSizereports the byte limit.- File identifiers and downloads explains file ID, resource ID, type enum, stored URL and the different content response shapes.