GitHub - flowjs/ng-flow: Flow.js html5 file upload extension on angular.js framework (original) (raw)

Saucelabs Test Status

View angular2+ repo at https://github.com/flowjs/ngx-flow

ng-flow is a Flow.js extensions for angular.js framework, no 3rd party JS dependencies required!

Demo: http://flowjs.github.io/ng-flow/

How can I install it?

  1. Get the library:

Direct DownloadDownload a latest build from https://github.com/flowjs/ng-flow/releasesit contains development and minified production files in dist/ directory, they are also concatenated with core flow.js library.

Using NPM

    npm install @flowjs/ng-flow --save

Using Bower

Git Clone

    git clone https://github.com/flowjs/ng-flow

Using Yeoman

    bower install "ng-flow#~2" --save
    grunt bower-install
  1. Add the module to your app as a dependency:
 angular.module('app', ['flow'])  
  1. Include the files in your project

How can I use it?

First of all wrap places there you are going to use Flow.js

... other flow directives goes here ...

This directive is going to add flowvariabletocurrentscope.Alsodirectivecanbenested,because‘flow variable to current scope. Also directive can be nested, because </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">iab</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal">oc</span><span class="mord mathnormal">u</span><span class="mord mathnormal">rre</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">sco</span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span><span class="mord">.</span><span class="mord mathnormal">A</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">so</span><span class="mord mathnormal">d</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rec</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">anb</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">es</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">a</span><span class="mord mathnormal">u</span><span class="mord mathnormal">se</span><span class="mord">‘</span></span></span></span>flow variable is going to be overridden.$flow is instance of Flow.

Secondly you need to assign some upload buttons:

Input OR Other element as upload button Upload File

First button is for normal uploads and second is for directory uploads. Note: avoid using <a> and <button> tags as file upload buttons, use <span> instead.

Now you need to display uploaded files, all you need to do is to loop files array. Files array is attached to flow object named $flow.

{{$index+1}} {{file.name}}

file is instance of FlowFile.

Quick setup

Input OR Other element as upload button Upload File
{{$index+1}} {{file.name}} {{file.msg}}

Need more examples?

Clone this repository and go to "ng-flow/samples/basic/index.html". Single image upload "ng-flow/samples/image/index.html".

How can I drop files?

Use flow-drop directive:

Drag And Drop your file here

Note: in most cases flow-drop must be used together with flow-prevent-drop directive on bodyelement, because it prevents file from being loaded in the browser.

Prevent dropping files on a document

Use flow-prevent-drop directive on body element:

How to add some styles while dropping a file?

Use flow-drag-enter directive:

Note: flow-drag-leave attribute can't be used alone, it is a part of flow-drag-enter directive.

How to dynamically disable drop area?

Drag And Drop your file here

See example at samples/dataurl/.

How can I preview uploaded image?

Use flow-img directive:

Image will be automatically updated once file is added. No need to start upload.

How can I set options for flow.js?

Use config:

var app = angular.module('app', ['flow']) .config(['flowFactoryProvider', function (flowFactoryProvider) { flowFactoryProvider.defaults = { target: '/upload', permanentErrors:[404, 500, 501] }; // You can also set default events: flowFactoryProvider.on('catchAll', function (event) { ... }); // Can be used with different implementations of Flow.js // flowFactoryProvider.factory = fustyFlowFactory; }]);

also can be configured on "flow-init" directive:

How can I catch events?

Events are listed inside flow-init directive:

file,file, file,message, $flow )" flow-file-progress="someHandlerMethod( file,file, file,flow )" flow-file-added="someHandlerMethod( file,file, file,event, $flow )" flow-files-added="someHandlerMethod( files,files, files,event, $flow )" flow-files-submitted="someHandlerMethod( files,files, files,event, $flow )" flow-file-retry="someHandlerMethod( file,file, file,flow )" flow-file-error="someHandlerMethod( file,file, file,message, $flow )" flow-error="someHandlerMethod( file,file, file,message, $flow )" flow-complete=" ... " flow-upload-started=" ... " flow-progress=" ... " >

How can I catch an event in a controller?

If controller is on the same scope as flow-init directive or in a child scope, then we can catch events with $on. Events are prefixed with flow::. scope.scope.scope.on('flow::fileAdded', function (event, $flow, flowFile) { event.preventDefault();//prevent file from uploading });

second argument is always a flow instance and then follows event specific arguments.

How can I assign flow to a parent scope?

Use flow-name attribute and set it to any variable in the scope.

... Flow is set to obj.flow ... I have uploaded files: #{{obj.flow.files.length}}

$scope.obj = {}; // variable "obj" must be initialized on the scope

How can I initialize flow with an existing flow object ?

Use flow-object attribute and set it with the existing flow object on scope.

... Flow is initialized with existingFlowObject, no new Flow object is created ... There are already {{ existingFLowObject.files.length }} files uploaded, which is equal to {{ $flow.files.length }}.

How can I support older browsers?

Go to https://github.com/flowjs/fusty-flow.jsand add to your config:

var app = angular.module('app', ['flow']) .config(['flowFactoryProvider', function (flowFactoryProvider) { flowFactoryProvider.factory = fustyFlowFactory; }]);

Contribution

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