Upload a file

Upload files for attaching to messages


Overview #

Upload a file to Cord's file storage for use in other Cord APIs, such as attaching to a message. Because uploading the file contents may take a long time, this works in two steps. First, the file record is created, and then the file is uploaded directly from the browser to the file storage. You can reference the file in other APIs as soon as the first step is complete.
Certain types of files, such as executable code, cannot be uploaded. Trying to do so will generate an error.
Files that are uploaded but never attached to a message will eventually be garbage collected.
Vanilla JavaScript:
const { id } = await window.CordSDK.file.uploadFile({
  name: myFileInput.files[0].name,
  blob: myFileInput.files[0],
});
const { id } = await window.CordSDK.file.uploadFile({
  name: myFileInput.files[0].name,
  blob: myFileInput.files[0],
});
Copy

What this function returns #

A promise that resolves once the file has been allocated an ID by the backend. At that point, it is safe to reference the file in other APIs, though the file may not be fully uploaded to the file storage yet and could still fail. If you want to know when the file upload is fully complete, you can await the uploadPromise property on the return value.

Arguments this function takes #


data #

required
ClientCreateFile
The file to upload.

This is an object with the following fields:


name #

required
string
The name of the file. This will be shown to the user when attached to a message and will be the file's name if it's downloaded.

blob #

required
Blob
The file contents, such as from a file input element.

Ask Cordy