File uploader
Here you can find a set of ready-made uploaders for the most frequent file uploading use-cases. Each uploader is highly customizable on its own and could be used as a custom build reference, or you can use it as is.
On this page you'll find
Solutions
- Regular case â the most common solution.
- Inline case â no modals, could be used inline.
- Minimal case â minimal and compact.
Implementation
ð Solution benefits
- No heavy dependencies, modern and efficient technologies under the hood.
- Uniform and seamless integration flow for all major web development stacks.
- CSP (Content Security Policy) compatible: no
unsafe-inline
flags for the CSS or JavaScript are needed. - Multiple file sources (local, camera, external URLs, cloud services and social networks) support.
- Open source (MIT license).
- Built with love âĪïļ
ð Supported browsers
Blocks are supported and tested in all major modern desktop and mobile browsers:
- Chrome
- Firefox
- Safari
- Edge
- Opera
Internet Explorer is outdated and not supported anymore.
Regular uploader
<lr-file-uploader-regular></lr-file-uploader-regular>
Inline uploader
<lr-file-uploader-inline></lr-file-uploader-inline>
Minimalistic uploader
<lr-file-uploader-minimal></lr-file-uploader-minimal>
ð Configuration
General
All configurations, localization texts, icons, and styling are placed into CSS file (if you aren't familiar with this concept, read about blocks configuration), so you should connect the default one (or create your own):
<style>
@import url(https://unpkg.com/@uploadcare/blocks@0.30.4/web/file-uploader-regular.min.css);
.my-settings {
--ctx-name: 'my-uploader';
--cfg-pubkey: 'demopublickey';
}
</style>
<lr-uploader class="my-settings lr-wgt-common"> </lr-uploader>
lr-wgt-common
â is a pre-defined common CSS class containing all basic uploader parameters.
There are two major parameters you should know:
- Your Uploadcare's project public key:
--cfg-pubkey: 'YOUR_PUBLIC_KEY';
. - Workflow context name:
--ctx-name: 'CONTEXT_NAME';
.
Public key
You should get a Public API Key in your Uploadcare project's dashboard to use file uploading features.
For demo-only purposes, you can use demopublickey
instead.
Context name
Our concept of workflow contexts is very similar to native HTML name
attributes for the "radio" inputs. When you use the same names, elements act in one common context. In the case of "radio" inputs, all elements in the same context will know the state of the others (and you can make only one possible selection).
In the case of uploader, you can also set the context with a ctx-name
attribute or --ctx-name
custom CSS property. This helps to create the link between each uploader instance and its internal or external entities. By default, context is created automatically, but if you need to bind uploader to some other workflow, you can use the following approach:
...
<lr-uploader ctx-name="my-uploading-workflow"></lr-uploader>
...
<lr-data-output ctx-name="my-uploading-workflow"></lr-data-output>
...
For more information, read about "context" in Symbiote.js documentation.
CSS custom properties
All configurations could be provided via the set of CSS-variables:
.my-settings {
--cfg-pubkey: 'demopublickey';
--cfg-multiple: 1;
--cfg-img-only: 0;
--cfg-source-list: 'local, url, camera, dropbox, gdrive, facebook';
...;
}
The variable value should be a correct JSON value. Strings should be taken in quotes. We use the 1
or 0
numbers to define boolean flags.
Any configuration value can be defined and redefined at any DOM-tree level regarding CSS selector specificity.
Parameters description
Name | Description | Values | Default |
---|---|---|---|
--cfg-pubkey |
Your project Public Key | string | YOUR_PUBLIC_KEY |
--cfg-multiple |
Allow to upload multiple files | 1 or 0 |
1 |
--cfg-multiple-min |
Minimum number of files that can be selected | number | none |
--cfg-multiple-max |
Maximum number of files that can be selected | number | none |
--cfg-confirm-upload |
Enables user confirmation for upload starting | 1 or 0 |
1 |
--cfg-img-only |
Accept images only | 1 or 0 |
0 |
--cfg-accept |
Native file input accept attribute value | comma separated string | none |
--cfg-external-sources-preferred-types |
Defines the list of preferred MIME types for external sources. See docs for details | comma separated string | none |
--cfg-store |
Store files | 1 or 0 |
- |
--cfg-camera-mirror |
Flip camera image | 1 or 0 |
0 |
--cfg-source-list |
Comma-separated list of file sources. See available sources below | comma separated string` | 'local, url, camera, dropbox, gdrive' |
--cfg-max-local-file-size-bytes |
Maximum file size in bytes | number | none |
--cfg-thumb-size |
Image thumbnail size | 76 |
76 |
--cfg-show-empty-list |
Show uploads list when it's empty | 1 or 0 |
0 |
--cfg-use-local-image-editor |
Enable local image editing | 1 or 0 |
0 |
--cfg-use-cloud-image-editor |
Enable cloud image editing | 1 or 0 |
0 |
--cfg-remote-tab-session-key |
Key to revoke Custom OAuth access. See docs for details | string | none |
--cfg-cdn-cname |
Set Custom CNAME. See docs for details | string | 'https://ucarecdn.com' |
--cfg-base-url |
Set custom upload URL | string | 'https://upload.uploadcare.com' |
--cfg-secure-signature |
Set signature for Secure Uploads. See docs for details |
string | none |
--cfg-secure-expire |
Set expire for Secure Uploads. See docs for details |
string | none |
--cfg-secure-delivery-proxy |
Set proxy URL template for Secure Delivery. See here for details | string | none |
--cfg-group-output |
Enables files group creation | 1 or 0 |
0 |
--cfg-remove-copyright |
Remove copyright | 1 or 0 |
0 |
--cfg-source-list
local
url
camera
dropbox
gdrive
facebook
gphotos
instagram
flickr
vk
evernote
box
onedrive
huddle
Example
.my-configuration {
--cfg-source-list: 'url, local, instagram';
}
--cfg-secure-delivery-proxy
The parameter can be used with signed URLs. Defines template for your proxy backend URL.
This is replacement for the File Uploader v3 previewProxy
option.
NOTE: There is no replacement for File Uploader v3 previewUrlCallback
option. If you need such functionality, please create a feature request.
Value for --cfg-secure-delivery-proxy
is a string template with the following variables:
previewUrl
That means that you can use {{previewUrl}}
in your template to insert the URL of file to proxify.
Example
.my-configuration {
--cfg-secure-delivery-proxy: 'https://domain.com/preview?url={{previewUrl}}';
}
CSP settings
If the application works with sensitive user data (e.g personal photos), it is recommended to increase its security with CSP settings. Uploader is using Blob
URLs for on-the-flight generated images and the stylesheets in some cases, so don't forget to add blob:
source into the CSP settings:
<meta
http-equiv="Content-Security-Policy"
content="style-src 'self' blob:; script-src 'self'; img-src 'self' https://ucarecdn.com blob:;"
/>
ð Styling
For the look & feel customization, you can use the "Elements" section in your browser developer tools panel. It's easy to find any uploader inner components and their contents because they have custom tag names. You don't need any specific tools, unlike with libraries like React. These tag names could be used as convenient CSS selectors.
There are three major levels of possible styling customizations:
- Light and dark theme flag:
--darkmode:1;
(enabled) or--darkmode:0;
(disabled). - The set of basic CSS variables used for the other styling calculations.
- Custom CSS rules for each element.
Basic theme for blocks
There are 4 levels of abstraction:
HSL color space
We use HSL color space because it allows us to easily calculate derivative colors. That's what prefixes --h-
, --s-
, and --l-
are stand for.
Quick styling
In most cases switching the dark mode on or off and changing the accent color is enough to make block match your design.
--darkmode: 1;
--h-accent: 211;
--s-accent: 100%;
--l-accent: calc(50% - 5% * var(--darkmode));
Base values
--darkmode
â1
: dark mode enabled,0
: disabled;--*-foreground
â text color, borders, and shaders. It should be in contrast to the background;--*-background
â background color and its variations;--*-accent
â colors of buttons, links, and text input borders (hover and focus). It should be in contrast to the background;--*-confirm
â the color of confirmation notifications and badges;--*-error
â the color of error notifications and badges;--opacity-*
â opacity of different states of small icon buttons;--ui-size
â minimum size of a clickable element. Also used to calculate the size of the elements, which should be proportional to it;--gap-*
â paddings and margins;--gap-table
â the gap between elements in lists (for example, in upload-list);--borders
â1
: borders enabled,0
: disabled. Can be fractional, for example, 0.5 will make borders half as opaque;--border-radius-element
â border radius of buttons and inputs;--border-radius-frame
â border radius of modal windows and drop area;--border-radius-thumb
â border radius of thumbnails;--transition-duration
â duration of all animated transitions;--shadows
â1
: shadows enabled,0
: disabled. Can be fractional, for example 0.5, will make shadows half as opaque;--*-shadow
â the color of box shadows;--modal-max-w
,--modal-max-h
â the maximum size of the modal window.
Derivative values
Derivative values are calculated from the base values.
--darkmode-minus
â used for dark mode color calculations. Gives-1
when dark mode is enabled,1
when disabled;--clr-background*
â lightness variations of the background color;--clr-accent*
â opacity variations of the accent color;--clr-confirm
â confirmation notifications color;--clr-error
â opacity and lighness variations of the error notifications color;--clr-txt*
â lightness variations of the text color;--clr-shade-lv*
â shading colors (foreground color with a low opacity variations);--border-*
â border variations;--clr-curtain
â color of the background behind the modal window;--clr-btn-bgr-primary*
,--clr-btn-txt-primary
,--shadow-btn-primary
â primary action button values;--clr-btn-bgr-secondary*
,--clr-btn-txt-secondary
,--shadow-btn-secondary
â secondary action button values;--clr-btn-bgr-disabled
,--clr-btn-txt-disabled
,--shadow-btn-disabled
â disabled button values.
Common styles
Common styles define similar UI elements across different blocks: buttons, inputs, and links.
Component styles
Component styles are the most specific.
Shadow DOM
If you need additional isolation and styling security levels, you can get it with Shadow DOM.
To enable it and encapsulate all styles into separated scope, use the css-src
attribute:
<lr-uploader css-src="https://unpkg.com/@uploadcare/blocks@0.30.4/web/file-uploader-regular.min.css">
</lr-uploader>
ðŠĪ Data handling
With the data output
We provide the dedicated block for the data output purposes â <lr-data-output>
. This Custom Element can be connected to some workflow context and provide you with convenient data access.
Here is the code example:
<template id="output-template">
<h3>Files uploaded:</h3>
<div repeat="filesData">
<lr-img width="300" set="@uuid: uuid"></lr-img>
<div><a set="@href: cdnUrl">{{cdnUrl}}</a></div>
</div>
</template>
<lr-data-output use-console use-event use-template="#output-template"> </lr-data-output>
Let's walk through its attributes:
use-console
â enables browser console output without modifying the source code.use-event
â enables custom events (data-output
) dispatching for the DOM element. These events contain all uploading data and could be processed at any level of your application.use-group
- create group from uploaded files, the same as--cfg-group-output
.use-template
â uploading results could be rendered as a list of nested DOM elements. You can specify a simple template for that.use-input
â create input to be used inside HTML-form.input-name
â used together withuse-form
. Sets the input name. The context name will be used by default.input-required
â whether the input is required or not. Works as the nativerequired
attribute.
With the event listener
window.addEventListener('LR_DATA_OUTPUT', (e) => {
console.log(e.detail);
});
ðĨ Events
Upload flow events
LR_UPLOAD_START
â upload is started for the file list selected by the userLR_REMOVE
â fired when one of the uploaded items is removed from the uploading listLR_UPLOAD_PROGRESS
â common upload progress for the listLR_UPLOAD_FINISH
â uploading is finishedLR_UPLOAD_ERROR
â error ocurred during file uploadingLR_VALIDATION_ERROR
â file fails checks according to the validation settingsLR_CLOUD_MODIFICATION
â image was modified via cloud APILR_DATA_OUTPUT
â common data about uploads
You can catch all events in window scope:
window.addEventListener('LR_UPLOAD_START', (e) => {
console.log(e.detail);
});
To define what exact workflow caused an event, use the context name:
...
<lr-file-uploader-regular ctx-name="UPLOADER_1"></lr-file-uploader-regular>
...
<lr-file-uploader-regular ctx-name="UPLOADER_2"></lr-file-uploader-regular>
...
window.addEventListener('LR_UPLOAD_START', (e) => {
if (e.detail.ctx === 'UPLOADER_1') {
console.log('Uploading started in the FIRST uploader instance.', e.detail.data);
} else if (e.detail.ctx === 'UPLOADER_2') {
console.log('Uploading started in the SECOND uploader instance.', e.detail.data);
}
});
Activities
Activity is a current user interaction stage focused on the uploader application. It helps manage the visibility of components and switches between several UI states. To create an activity, you should register it in your custom block:
import { LR } from '@uploadcare/blocks';
class MyBlock extends LR.BlockComponent {
initCallback() {
const onActivate = () => {
console.log('activity-name is activated');
};
const onDeactivate = () => {
console.log('activity-name is deactcivated');
};
this.registerActivity('my-activity-name', {
onActivate,
onDeactivate,
});
}
}
Then, if some other component will call the registered activity, it will be activated with an active
attribute, and the activation callback will be called.
JavaScript:
import { LR } from '@uploadcare/blocks';
class MyOtherBlock extends LR.BlockComponent {
onclick = () => {
this.$['*currentActivity'] = 'my-activity-name';
};
}
Resulting HTML:
<lr-my-block activity="my-activity-name" active>...</lr-my-block>
Then you can use lr-my-block[active]
selector to specify visibility or animations with CSS.
Here is the list of reserved pre-defined activities:
source-select
camera
upload-list
url
confirmation
cloud-image-edit
*external
details