JavaScript API — File groups

  • This documentation is for the version 3.6.0 of the widget. If you're looking for the JavaScript for Widget v2 docs, check here.

A group is an ordered collection of file instances. Groups are immutable: once created, they can not be changed. However, you do not have to create a new group every time you delete a file: groups can hold null values.

Create a new group instance

You can create a new group from an array of files and/or other groups.

var fileGroup = uploadcare.FileGroup([file1, fileGroup1, file2]);

After a group instance is created, it can be pushed to Uploadcare by calling the promise method.

Get group info

You can subscribe to group upload events to let your app know when all files are uploaded, and a new group is created.

var groupPromise = fileGroup.promise();
groupPromise.done(function(fileGroupInfo) {
  // Upload successfully completed and all files in the group are ready.
});
groupPromise.fail(function(error, fileGroupInfo) {
  // Upload failed, or something else went wrong.
});

There is also a way to check out you group upload progress. Make use of the progress function,

groupPromise.progress(function(uploadInfo) {
  // State of upload is updated.
  // This callback is invoked at least once with current state,
  // immediately after assignment.
});

Get files in group as array

Here is how you get files stored in a group as array,

var arrayOfFiles = fileGroup.files();

Get file info on a first-ready-first-served basis,

$.each(arrayOfFiles, function(i, file) {
  // Wait for file uploading.
  file.done(function(fileInfo) {
    // i is file positon in group.
    console.log(i, fileInfo);
  });
});

Get file info in line with their positions in a group,

$.when.apply(null, arrayOfFiles).done(function() {
  // Info for each file will be passed as an argument.
  var fileInfos = arguments;
  $.each(fileInfos, function(i, fileInfo) {
    // i is file position in the group.
    console.log(i, fileInfo);
  });
});

Get file groups from CDN

Our JavaScript API provides a method to load a file group from CDN via either its identifier or CDN URL.

uploadcare.loadFileGroup('cdf2fe62-3d43-47d4-9288-c3951561f5c7~2')
  .done(function(fileGroup) {
    // Group creation completed successfully.
  })
  .fail(function() {
    // Something went wrong.
  });

Object overview, fileGroupInfo

This is how the structure of fileGroupInfo looks like,

PropertyTypeValue
uuidstring

File group identifier.

namestringLocalized group size string, e.g. “5 files”.
sizenumberTotal file size in bytes.
isStoredboolean

true if all files in a group are stored, false otherwise.

isImageboolean

true if all files in a group are images, false otherwise.

cdnUrlstring

File group CDN URL.

We’re always happy to help with code, integration, and other stuff. Search our site for more info or post your question in our Community Area.