File Uploader installation

From CDN

We recommend using the one of the modern code distribution services, such as:

Import the File Uploader library into your JavaScript code:

1<script type="module">
2 import * as UC from "https://cdn.jsdelivr.net/npm/@uploadcare/file-uploader@v1/web/file-uploader.min.js";
3 UC.defineComponents(UC);
4</script>

Note that we distribute the library as ES modules, so you need to use the type="module" attribute on the <script> tag.

We’re also providing IIFE bundle, but it’s not recommended to use it directly.

1<script src="https://cdn.jsdelivr.net/npm/@uploadcare/file-uploader@v1/web/file-uploader.iife.min.js" async></script>
2<script>
3 UC.defineComponents(UC);
4</script>

From NPM

Install the npm package:

1npm i @uploadcare/file-uploader

Then you can use the File Uploader and build your own solutions from the source code:

1import * as UC from '@uploadcare/file-uploader'
2
3UC.defineComponents(UC)

React Uploader

This library allows you to easily integrate the File Uploader into your React applications:

1npm i @uploadcare/react-uploader

Dynamic script connection

There is an alternative way if your project meets the following criteria:

  • Does not support new language features (such as nullish coalescing operator, static class properties) — for instance, this might apply to projects built with frameworks such as Nuxt 2. (Note that Nuxt 3 supports modern JavaScript features and works as needed).
  • Has SSR and does not support node conditional exports.

Then you can use this workaround to import the uploader only at the browser runtime, bypassing your project’s build system and obtaining a working type system. This can be achieved without the need to manually configure the build process (using tools such as Babel) or disabling SSR for a specific package.

First, install the npm package:

1npm i @uploadcare/file-uploader

Then import loadFileUploaderFrom function to connect the library dynamically and avoid errors:

1import { loadFileUploaderFrom } from '@uploadcare/file-uploader/abstract/loadFileUploaderFrom.js';
2
3loadFileUploaderFrom('https://cdn.jsdelivr.net/npm/@uploadcare/file-uploader@v1/web/file-uploader.iife.min.js');

You may need to implement some logic that depends on connected uploader or get access directly to the imported components. Since loadFileUploaderFrom returns Promise, use .then()

1import { loadFileUploaderFrom } from '@uploadcare/file-uploader/abstract/loadFileUploaderFrom.js';
2
3loadFileUploaderFrom('https://cdn.jsdelivr.net/npm/@uploadcare/file-uploader@v1/web/file-uploader.iife.min.js').then(
4 (UC) => {
5 if (!UC) {
6 return; // To avoid errors in SSR case
7 }
8
9 // Now you can realize your logic, e.g.:
10 const uploader = new UC.FileUploaderRegular();
11 document.body.appendChild(uploader);
12 }
13);

Choose a solution

To use the File Uploader in your application markup, select the tag that best fits your needs from the table below and place it in your HTML document:

ModeSyntax
Regular mode<uc-file-uploader-regular> </uc-file-uploader-regular>
Inline mode<uc-file-uploader-inline> </uc-file-uploader-inline>
Minimal mode<uc-file-uploader-minimal> </uc-file-uploader-minimal>

Look at the File Uploader solutions for more.

Example:

1<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@uploadcare/file-uploader@v1/web/uc-file-uploader-regular.min.css" />
2
3<uc-config
4 ctx-name="my-uploader"
5 pubkey="YOUR_PUBLIC_KEY"
6></uc-config>
7
8<uc-file-uploader-regular
9 ctx-name="my-uploader"
10></uc-file-uploader-regular>

The <uc-config> block is used to configure the uploader. Take a look at uploader configuration.

The ctx-name attribute is used to specify the name of the uploader context, which allows wire blocks together. Required.

The headless attribute has been introduced to control the visibility of a button in the regular mode uc-file-uploader-regular. By setting this attribute, you can easily toggle the button’s visibility between hidden and visible states. Optional.