Show examples in:
Create a new upload

The DatoCMS clients provide numerous methods for users to upload resources. The method you choose can be influenced by different aspects like the platform you're using (such as Node.js or a browser) and where the resource is coming from — like a local file, a remote URL, or a File or Blob obtained from <input type="file" /> elements.

Example Node.js: Create an upload from a local file
Example Node.js: Create an upload from a remote URL
Example Browser: Create an upload from a File or Blob object
Example Monitoring the progress

Each available method yields a cancellable promise, granting the ability to halt a currently running upload operation.

Example Cancelling an in-progress upload

It is possible to cancel an upload operation by calling the .cancel() method on the promise returned by one of the upload creation methods (createFromUrl(), createFromLocalFile() in NodeJS, createFromFileOrBlob() in browser):

import { buildClient, CanceledPromiseError } from "@datocms/cma-client-browser";
// Make sure the API token has access to the CMA, and is stored securely
const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN });
let cancelablePromise = null;
document.querySelector("button").addEventListener("click", () => {
if (cancelablePromise) {
cancelablePromise.cancel();
}
});
document
.querySelector('input[type="file"]')
.addEventListener("change", async (event) => {
cancelablePromise = client.uploads.createFromFileOrBlob({
fileOrBlob: event.target.files[0],
});
cancelablePromise
.then((upload) => {
cancelablePromise = null;
console.log(upload);
})
.catch((e) => {
if (e instanceof CanceledPromiseError) {
console.log("User canceled the upload process!");
} else {
throw e;
}
});
});

Body Parameters

id  string  Optional
RFC 4122 UUID of upload expressed in URL-safe base64 format
path  Required  string  Example: "/45/1496845848-digital-cats.jpg"

Upload path

copyright  Optional  string, null  Example: "2020 DatoCMS"

Copyright

author  Optional  string, null  Example: "Mark Smith"

Author

notes  Optional  string, null  Example: "Nyan the cat"

Notes

default_field_metadata  Optional  object  Example: {"en":{"title":"this is the default title","alt":"this is the default alternate text","custom_data":{"foo":"bar"},"focal_point":{"x":0.5,"y":0.5}}}

For each of the project's locales, the default metadata to apply if nothing is specified at record's level.

tags  Optional  Array<string>  Example: ["cats"]

Tags

Returns

Returns a upload resource object.