Skip to content

k.file.resumableUpload ​

Chunked file uploads and resumable upload task management

Execution ContextPrerequisitesTypical Use
Server-side KScript (k.api)Chunk indexes are 1-based; minimum chunkSize is 128 * 1024 (128 KiB); build() removes the temp task folder on completionUpload large files in chunks, track remaining requireChunks, and assemble the final file on disk

Quick Start ​

Create a resumable upload task, save a chunk, and assemble the final file:

js
k.api.post(() => {
    const target = "uploads/demo.bin"
    if (k.file.exists(target)) {
        k.file.delete(target)
    }
    const payload = [65, 66, 67, 68]
    const task = k.file.resumableUpload.create("demo.bin", payload.length, 256 * 1024)
    task.saveChunk(payload, 1)
    const fileInfo = task.build(target)
    return {
        fullName: fileInfo.fullName,
        size: fileInfo.size,
        url: fileInfo.relativeUrl
    }
})

Quick Reference ​

Method / PropertyDescriptionReturns
create(name, size, chunkSize?)Creates a resumable upload task (minimum chunkSize 128 * 1024)ResumableUploadTask
get(id)Looks up a task by ID (throws if missing or already built)ResumableUploadTask
remove(id)Task removal endpoint (no-op in current runtime; see warning below)void
task.saveChunk(file | bytes, chunk)Saves a 1-based chunk from an UploadFile or byte arrayvoid
task.build(fileName?)Assembles all chunks into the target disk file (does not overwrite) and deletes the temp folderFileInfo
task.requireChunksList of remaining 1-based chunk indexesnumber[]

Chunk indexing & lifecycle notes

  1. Chunk indexes start at 1 (requireChunks is [1, 2, ...]), not 0.
  2. The minimum chunkSize is 128 * 1024 (128 KiB); smaller values are automatically raised to 131072.
  3. Calling task.build() after all chunks are saved assembles the target file and automatically cleans up the temporary task directory (if the target file already exists, IOException is thrown and the temp folder is not removed).

Task Management Methods ​

create() ​

Creates a resumable upload task.

ParameterTypeRequiredDescription
namestringYesFile name
sizenumberYesTotal file size (bytes)
chunkSizenumberNoChunk size; defaults to 1024 * 1024, minimum 128 * 1024

Returns: ResumableUploadTask

js
k.api.post("create", () => {
    const task = k.file.resumableUpload.create("video.mp4", 10 * 1024 * 1024, 1024 * 1024)
    return {
        id: task.id,
        name: task.name,
        size: task.size,
        chunkSize: task.chunkSize,
        requireChunks: task.requireChunks
    }
})

get() ​

Gets a resumable upload task by id. Throws "Resumable upload task not found" if the task does not exist (or after task.build() has completed and cleaned it up).

ParameterTypeRequiredDescription
idstringYesTask ID

Returns: ResumableUploadTask

js
k.api.post("get", () => {
    const task = k.file.resumableUpload.create("video.mp4", 1024 * 1024, 256 * 1024)
    const retrieved = k.file.resumableUpload.get(task.id)
    return { id: retrieved.id, name: retrieved.name, requireChunks: retrieved.requireChunks }
})

remove() ​

Removes a resumable upload task.

Current runtime behavior

In the current runtime, k.file.resumableUpload.remove(id) checks the task directory path as a regular file and therefore acts as a no-op, so k.file.resumableUpload.get(id) can still read the task afterward. Temporary task directories are automatically deleted once task.build() succeeds.

ParameterTypeRequiredDescription
idstringYesTask ID

Returns: void

js
k.api.post("remove", () => {
    const task = k.file.resumableUpload.create("video.mp4", 1024 * 1024, 256 * 1024)
    k.file.resumableUpload.remove(task.id)
    return { taskId: task.id }
})

ResumableUploadTask ​

Resumable upload task object.

Properties ​

PropertyDescriptionType
idTask IDstring
nameFile namestring
sizeFile size (bytes)number
chunkSizeChunk size (bytes)number
requireChunksList of remaining chunk indexes (1-based)number[]
timestampCreation timestamp (Unix seconds)number

build() ​

Assembles the final file from all saved chunks and deletes the temporary task directory upon success. Throws an error if any chunks remain in requireChunks.

Does not overwrite existing files

build() moves the assembled file without overwriting. If the destination file already exists, it throws IOException and leaves the temporary task directory uncleaned. Delete any existing file first with k.file.delete(fileName) or pass a unique target path.

ParameterTypeRequiredDescription
fileNamestringNoDestination file path; defaults to the task's original file name

Returns: FileInfo

js
k.api.post("build", () => {
    const payload = [65, 66, 67, 68]
    const task = k.file.resumableUpload.create("demo.bin", payload.length, 256 * 1024)
    task.saveChunk(payload, 1)
    const target = `uploads/demo-${task.id}.bin`
    const fileInfo = task.build(target)
    return { fullName: fileInfo.fullName, size: fileInfo.size, url: fileInfo.relativeUrl }
})

saveChunk() ​

Saves one uploaded chunk, accepting either a byte array (number[]) or an uploaded UploadFile object from k.request.files (chunk indexes are 1-based).

Parameters (Overload 1 — Uploaded file object saveChunk(file, chunk), recommended for multipart uploads):

ParameterTypeRequiredDescription
fileUploadFileYesUploaded file object from k.request.files[i]
chunknumberYesChunk index (1-based)

Parameters (Overload 2 — Byte array saveChunk(bytes, chunk)):

ParameterTypeRequiredDescription
bytesnumber[]YesChunk byte array
chunknumberYesChunk index (1-based)

Returns: void

Overload 1 (uploaded file object):

js
k.api.post("saveChunkFile", () => {
    const taskId = k.request.queryString.get("taskId")
    const chunk = Number(k.request.queryString.get("chunk"))
    const file = k.request.files[0]

    const task = k.file.resumableUpload.get(taskId)
    task.saveChunk(file, chunk)

    const updated = k.file.resumableUpload.get(taskId)
    return { saved: true, requireChunks: updated.requireChunks }
})

Overload 2 (byte array):

js
k.api.post("saveChunkBytes", () => {
    const task = k.file.resumableUpload.create("sample.bin", 4, 256 * 1024)
    task.saveChunk([65, 66, 67, 68], 1)

    const updated = k.file.resumableUpload.get(task.id)
    return { taskId: task.id, requireChunks: updated.requireChunks }
})

TypeScript Definitions ​

ts
interface ResumableUpload {
  create(name: string, size: number, chunkSize?: number): ResumableUploadTask;
  get(id: string): ResumableUploadTask;
  remove(id: string): void;
}

interface ResumableUploadTask {
  id: string;
  name: string;
  size: number;
  chunkSize: number;
  requireChunks: number[];
  timestamp: number;
  build(fileName?: string): FileInfo;
  saveChunk(bytes: number[], chunk: number): void;
  saveChunk(file: UploadFile, chunk: number): void;
}