URLSessionUploadTask represents a background upload task instance.
It is created via BackgroundURLSession.startUpload() or BackgroundURLSession.resumeUpload() and allows file uploads to continue in the Scripting app, even when the app is in the background or the script is terminated.
Each upload task provides real-time state, progress, and event callbacks for tracking upload activity.
id: stringA unique identifier for the upload task. This value can be used to recognize and reattach to the same task after restarting a script.
Example
state: URLSessionTaskStateThe current state of the upload task.
Possible values:
"running" — The task is currently uploading"suspended" — The task is paused"canceling" — The task is being canceled"completed" — The task has completed"unknown" — The task’s state is unknown (usually if removed by the system)Example
progress: URLSessionProgressReal-time progress information for the upload task.
Contains the following fields:
fractionCompleted: number — Completion ratio between 0 and 1totalUnitCount: number — Total number of bytes expected to uploadcompletedUnitCount: number — Number of bytes uploaded so farisFinished: boolean — Whether the task has finishedestimatedTimeRemaining: number | null — Estimated remaining time in seconds (may be null)Example
priority: numberThe upload priority (range: 0.0–1.0).
Defaults to 0.5.
Higher values indicate a higher scheduling priority for the system.
Example
earliestBeginDate?: Date | nullSpecifies the earliest date and time when the upload task may begin. Useful for delaying uploads until conditions like network availability or power are favorable.
Example
countOfBytesClientExpectsToSend: numberA best-guess upper bound of bytes expected to send (for system estimation only).
countOfBytesClientExpectsToReceive: numberA best-guess upper bound of bytes expected to receive (for system estimation only).
onReceiveData?: (data: Data) => voidTriggered when response data is received from the server.
The data parameter contains binary data from the server.
Example
onComplete?: (error: Error | null, resumeData: Data | null) => voidTriggered when the upload task finishes, whether successfully or with an error.
Parameters
error: If the upload failed, contains the error object; otherwise null.resumeData: If the upload failed and can be resumed, contains resume data (Data); otherwise null.Example
suspend(): voidPauses the upload task.
A suspended task produces no network traffic and isn’t subject to timeouts.
Use resume() to continue the upload later.
Example
resume(): voidResumes a suspended upload task. Newly-initialized tasks begin in a suspended state, so you need to call this method to start the task.
Example
cancel(): voidCancels the upload task immediately.
The task enters the "canceling" state, and the onComplete callback is invoked with an error once cancellation finishes.
Example
suspend() and later resume it with resume().resumeData for recovery.BackgroundURLSession.getUploadTasks() to retrieve and reattach callbacks to ongoing uploads after restarting your script.notifyOnFinished allows a local notification to alert users when the upload completes.Authorization headers), ensure credentials are stored and managed securely.