jQuery File Uploader JavaScript API Reference

We’ve built the next version of the File Uploader.

Uploadcare jQuery File Uploader can handle either a single file upload or multiple files at once multi-upload. This section covers your JavaScript API uploading widget workflow and methods.

Uploading widget initialization

Initialize an uploading widget.

1var widgets = uploadcare.initialize('#my-form');
2widgets; // [widget1, widget2, multipleWidget1, ...]
3var widgets = uploadcare.initialize();

This is a one-time initialization of the uploading widget instances on every element with the role="uploadcare-uploader" attribute in a container specified by a selector or DOM element. Works for both single-file and multi-file uploading widgets.

Returns an array of newly initialized uploading widgets or an empty array in case no new instances were initialized.

The uploadcare.initialize() method is called automatically unless the manual start uploading widget option is defined.

Single-file uploading widgets

Get a single-upload uploading widget instance for a given element.

1var singleWidget = uploadcare.SingleWidget('[role=uploadcare-uploader]');

uploadcare.SingleWidget() element argument can be a DOM element, jQuery object, or CSS selector. If an argument is a CSS selector, the method returns only an instance for the first matched element. To ensure you are getting the right instance when dealing with multiple uploading widgets on a page, select by id or pass a DOM element to the method.

All uploading widget options will be loaded from data-attribute of your element on a first widget initialization.

Passing an element with the data-multiple attribute to uploadcare.SingleWidget() raises an error. A multi-upload uploading widget should be used instead.

Multi-file uploading widget

Get a multi-upload uploading widget instance for a given element with the data-multiple option set.

1var multiWidget = uploadcare.MultipleWidget('[role=uploadcare-uploader][data-multiple]');

The workflow is similar to single-upload uploading widget. If an instance of the first encountered element is not a multi-upload uploading widget, an error is raised.

Multi-purpose uploading widgets

Get either single-upload or multi-upload uploading widget instance depending on the passed data-multiple attribute. While the API for single-upload and multi-upload uploading widgets is slightly different, you can use this constructor when your code can work with both versions:

1var widget = uploadcare.Widget('[role=uploadcare-uploader]');

Pass a single file to a uploading widget

Set a file instance as a value for a single-uploaduploading widget.

1singleWidget.value(file);

You can also use a file UUID or CDN link as a value:

1singleWidget.value('9dd2f080-cc52-442d-aa06-1d9eec7f40d1');

Setting null as a value clears the uploading widget:

1singleWidget.value(null);

Pass multiple files to the uploading widget

Set a group instance as a value for a multi-upload uploading widget.

1multiWidget.value(fileGroup);

You can also set an array of file instances or UUIDs as a value for the multi-upload:

1multiWidget.value([file1, file2, '9dd2f080-cc52-442d-aa06-1d9eec7f40d1']);

Or, just pass a valid file group identifier:

1multiWidget.value('cdf2fe62-3d43-47d4-9288-c3951561f5c7~2');

Get a current file uploader value

Get current file uploader value: file instance, group instance or null.

1var file = widget.value();

Load a new file or group

Load a new file or group instance from the value attribute of your input or update the object info. You will get a UUID or CDN link.

1widget.reloadInfo();

Open a file uploader dialog

Open the dialog of a specific file uploader and get its instance. Returns a dialog API.

1var dialog = widget.openDialog(tab);

tab defines a name of the tab opened by default.

On change, file uploader event

Provides you with the ability to do something after a new file is selected. An instance is passed to the callback: a file for single-upload uploading widgets or a file group for multi-upload uploading widgets. The instance may or may not be uploaded.

1widget.onChange(function(file) {
2 // Handle new file.
3});

On upload complete, file uploader event

Provides you with the ability to do something after a file is uploaded and ready. The callback is passed an info object: a file info for single-upload uploading widgets or a file group info for multi-upload uploading widgets.

1widget.onUploadComplete(function(info) {
2 // Handle uploaded file info.
3});
4
5// Same as above:
6widget.onChange(function(file) {
7 if (file) {
8 file.done(function(info) {
9 // Handle uploaded file info.
10 });
11 };
12});

On dialog open, file uploader event

Provides you with the ability to do something on file uploader dialog open. Works with a user click or the widget.openDialog() call. Works only when the system dialog option is disabled.

1widget.onDialogOpen(function(dialogApi) {
2 dialogApi.switchTab('dropbox');
3});

Unsubscribe file uploader callbacks

Allows you to unsubscribe any file uploader callback:

1widget.onChange(fn); // Subscribe
2widget.onChange.remove(fn); // Unsubscribe
3
4widget.onUploadComplete(fn);
5widget.onUploadComplete.remove(fn);
6
7widget.onDialogOpen(fn);
8widget.onDialogOpen.remove(fn);