flowjs / flow.js

A JavaScript library providing multiple simultaneous, stable, fault-tolerant and resumable/restartable file uploads via the HTML5 File API.
Other
2.96k stars 346 forks source link

Flow.js Build Status Test Coverage Saucelabs Test Status Buy Me A Coffee

Flow.js is a JavaScript library providing multiple simultaneous, stable and resumable uploads via the HTML5 File API. (Demo)

The library is designed to introduce fault-tolerance into the upload of large files through HTTP. This is done by splitting each file into small chunks. Then, whenever the upload of a chunk fails, uploading is retried until the procedure completes. This allows uploads to automatically resume uploading after a network connection is lost either locally or to the server. Additionally, it allows for users to pause, resume and even recover uploads without losing state because only the currently uploading chunks will be aborted, not the entire upload.

Flow.js does not have any external dependencies other than the HTML5 File API. This is relied on for the ability to chunk files into smaller pieces. Currently, this means that support is limited to Firefox 4+, Chrome 11+, Safari 6+ and Internet Explorer 10+.

Samples and examples are available in the samples/ folder. Please push your own as Markdown to help document the project.

Can I see a demo?

Flow.js + angular.js file upload demo - ng-flow extension page https://github.com/flowjs/ng-flow

JQuery and node.js backend demo https://github.com/flowjs/flow.js/tree/master/samples/Node.js

How can I install it?

Download a latest build from https://github.com/flowjs/flow.js/releases it contains development and minified production files in dist/ folder.

or use npm:

npm install @flowjs/flow.js

or use bower:

bower install flow.js#~2

or use git clone

git clone https://github.com/flowjs/flow.js

How can I use it?

A new Flow object is created with information of what and where to post:

var flow = new Flow({
  target:'/api/photo/redeem-upload-token', 
  query:{upload_token:'my_token'}
});
// Flow.js isn't supported, fall back on a different method
if(!flow.support) location.href = '/some-old-crappy-uploader';

To allow files to be either selected and drag-dropped, you'll assign drop target and a DOM item to be clicked for browsing:

flow.assignBrowse(document.getElementById('browseButton'));
flow.assignDrop(document.getElementById('dropTarget'));

After this, interaction with Flow.js is done by listening to events:

flow.on('fileAdded', function(file, event){
    console.log(file, event);
});
flow.on('fileSuccess', function(file,message){
    console.log(file,message);
});
flow.on('fileError', function(file, message){
    console.log(file, message);
});

How do I set it up with my server?

Most of the magic for Flow.js happens in the user's browser, but files still need to be reassembled from chunks on the server side. This should be a fairly simple task and can be achieved in any web framework or language, which is able to receive file uploads.

To handle the state of upload chunks, a number of extra parameters are sent along with all requests:

You should allow for the same chunk to be uploaded more than once; this isn't standard behaviour, but on an unstable network environment it could happen, and this case is exactly what Flow.js is designed for.

For every request, you can confirm reception in HTTP status codes (can be change through the permanentErrors option):

Handling GET (or test() requests)

Enabling the testChunks option will allow uploads to be resumed after browser restarts and even across browsers (in theory you could even run the same file upload across multiple tabs or different browsers). The POST data requests listed are required to use Flow.js to receive data, but you can extend support by implementing a corresponding GET request with the same parameters:

After this is done and testChunks enabled, an upload can quickly catch up even after a browser restart by simply verifying already uploaded chunks that do not need to be uploaded again.

Full documentation

Flow

Configuration

The object is loaded with a configuration options:

var r = new Flow({opt1:'val', ...});

Available configuration options are:

Properties

Methods

Events

FlowFile

FlowFile constructor can be accessed in Flow.FlowFile.

Properties

Methods

Contribution

To ensure consistency throughout the source code, keep these rules in mind as you are working:

Installation Dependencies

  1. To clone your Github repository, run:
    git clone git@github.com:<github username>/flow.js.git
  2. To go to the Flow.js directory, run:
    cd flow.js
  3. To add node.js dependencies
    npm install

    Testing

Our unit and integration tests are written with Jasmine and executed with Karma. To run all of the tests on Chrome run:

grunt karma:watch

Or choose other browser

grunt karma:watch --browsers=Firefox,Chrome

Browsers should be comma separated and case sensitive.

To re-run tests just change any source or test file.

Automated tests is running after every commit at travis-ci.

Running test on sauceLabs

  1. Connect to sauce labs https://saucelabs.com/docs/connect
  2. grunt test --sauce-local=true --sauce-username=**** --sauce-access-key=***

other browsers can be used with --browsers flag, available browsers: sl_opera,sl_iphone,sl_safari,sl_ie10,sl_chrome,sl_firefox

Origin

Flow.js was inspired by and evolved from https://github.com/23/resumable.js. Library has been supplemented with tests and features, such as drag and drop for folders, upload speed, time remaining estimation, separate files pause, resume and more.