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.
Each available method yields a cancellable promise, granting the ability to halt a currently running upload operation.
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";const client = buildClient({ apiToken: "<YOUR_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;}});});
Upload path
Copyright
Author
Notes
For each of the project's locales, the default metadata to apply if nothing is specified at record's level.
Tags