jQuery File Uploader JavaScript API: Uploading Files
Uploadcare jQuery File Uploader and JavaScript API work with file instances, which implement the jQuery promise interface. You can create a file instance via a new upload or by addressing existing files.
Creating a new file instance
A new file instance can be constructed by calling the fileFrom
function. Where
the first argument is one of the predefined identifiers, and the second argument
holds a file source. Depending on the passed identifier, the function can start
a new upload or return an instance of an existing file.
Note, you can also pass a settings
object to
fileFrom
as its third parameter.
File instance from URL
Uploading a file from URL is done by passing the url
identifier to the
fileFrom
function:
File instance from native object
Our JavaScript API supports uploading new files from
native file objects. This is done by passing the
object
identifier to the fileFrom
function.
This method supports large file uploads (> 100MB) and checking upload progress. However, it will not work in outdated browsers, see browser support.
File instance from DOM
You can also upload files from input DOM elements. This is done by passing the
input
identifier to fileFrom
. Uploading from such inputs works in all browsers
but does not support large files and checking upload progress.
File instance from UUID or CDN URL
That is the case when a file instance is created from an existing file with
a UUID and hence a CDN URL. Any of those can be used as a file source
in fileFrom
.
The proper identifier to pass to the function here is uploaded
.
When using the uploaded
parameter, the widget fetches fileInfo
but doesn’t clone/reupload the file.
Upload multiple files
This is a batch wrapper around fileFrom
.
First argument is a source type: url
, object
, input
or uploaded
.
The second argument is an array of sources of a specified type.
Returns an array of file instances.
Wait until a file is available
Quite often, you want to ensure a file is there and ready before getting its instance. That is what the function is all about. It returns a fileInfo object.
The error
argument in the fail
callback is one of the following values,
or a user-defined custom value, raised via validators.
'baddata'
, invalid argument passed to file constructor'size'
, file size is too large'upload'
, error during upload'user'
, upload was canceled'info'
, error when fetching file information'deleted'
, file was deleted, also applicable to groups and files within
Checking the progress of a file upload
When handling larger files, there is a sense in checking their upload progress.
The progress
function does exactly that and returns an
uploadInfo object.
You can also show a file uploading progress via a jQuery UI progress bar.
Cancelling a file upload
Just use cancel
in case you want to immediately stop any in-progress upload and
all the events.
Object overview, fileInfo
Here is how a fileInfo
object is structured,
Object overview, uploadInfo
Here is how an uploadInfo
object is structured,