- Notifications
You must be signed in to change notification settings - Fork19
flow.js file upload for angular
License
flowjs/ngx-flow
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The purpose of this package is to create a wrapper for Angular for fileupload usingflow.js.
https://stackblitz.com/edit/ngx-flow-example
You can also find example source code in theprojects/ngx-flow-demo
folder.
- ✅ upload single file
- ✅ upload multiple files
- ✅ queue management
- ✅ error handling
- ✅ pause / resume upload
- ✅ cancel upload, cancel all uploads
- ✅ upload progress
- ✅ file / directory restrictions
- ✅ drag & drop
- ✅ display uploaded image
- ✅ tests
- ✅ upload right after selecting file
- ✅ run tests using TravisCI
- ✅ demo using Stackblitz
- ✅ support for server side rendering
From the v18, we aligned the main version of this library with Angular (and Angular CLI).
For previous versions, use the matrix below:
Angular | @flowjs/ngx-flow |
---|---|
17 | 0.8.1 |
16 | 0.7.2 |
15 | (not available) |
14 | 0.6.0 |
13 | 0.5.0 |
12 | (not available) |
6 -> 11 | 0.4.6 |
Install dependencies :
npm install @flowjs/flow.js @flowjs/ngx-flow
Import in your module:
import{NgxFlowModule,FlowInjectionToken}from'@flowjs/ngx-flow';importFlowfrom'@flowjs/flow.js';@NgModule({imports:[NgxFlowModule],providers:[{provide:FlowInjectionToken,useValue:Flow}]})exportclassAppModule
We use dependecy injection to provide flow.js library.
Start up server. There is a server example taken fromflow.js here in
server
directory. In this repo you can run it usingnpm run server
.First you need to initialize ngx-flow directive and export it as for example
flow
variable:<ng-container#flow="flow"[flowConfig]="{target: 'http://localhost:3000/upload'}"></ng-container>
Now you can use either directive
flowButton
for select file dialog:<inputtype="file"flowButton[flow]="flow.flowJs"[flowAttributes]="{accept: 'image/*'}"/>
Or
flowDrop
for drag&drop feature:<divclass="drop-area"flowDrop[flow]="flow.flowJs"></div>
For both you have to pass
[flow]=flow.flowJs
whereflow
is template variable exported in step 1.You can than subscribe to observable of transfers:
<div*ngFor="let transfer of (flow.transfers$ | async).transfers"></div>
After adding files you can begin upload using
upload()
method:<buttontype="button"(click)="flow.upload()"[disabled]="!(flow.somethingToUpload$ | async)">Start upload</button>
Observableflow.transfers$
emits state in form:
{totalProgress:0.5,transfers:[{name:"somefile.txt",flowFile:FlowFile,progress:number,error:boolean,paused:boolean,success:boolean,complete:boolean,currentSpeed:number,averageSpeed:number,size:number,timeRemaining:number,},{name:"uploading.txt",flowFile:FlowFile,progress:0.5,error:false,paused:false,success:false,complete:false,currentSpeed:number,averageSpeed:number,size:number,timeRemaining:number,},{name:"failed-to-upload.txt",flowFile:FlowFile,progress:number,error:true,paused:false,success:false,complete:true,currentSpeed:number,averageSpeed:number,size:number,timeRemaining:number,}{name: "uploaded.txt",flowFile:FlowFile,progress: number,error:false,paused:false,success:true,complete:true,currentSpeed: number,averageSpeed: number,size:number,timeRemaining: number,}],flow:{/* flow.js instance*/}}
You can find it underflow
variable.
<p>Is flowjs supported by the browser? {{flow.flowJs?.support}}</p>
UsetrackBy
forngFor
:
<div*ngFor="let transfer of (flow.transfers$ | async).transfers; trackBy: trackTransfer"></div>
exportclassAppComponent{trackTransfer(transfer:Transfer){returntransfer.id;}}
AddsingleFile: true
to your flow config:
<ng-container#flow="flow"[flowConfig]="{target: 'http://localhost:3000/upload', singleFile: true}"></ng-container>
AddflowDirectoryOnly="true"
to your button:
<inputtype="file"flowButton[flow]="flow.flowJs"flowDirectoryOnly="true"[flowAttributes]="{accept: 'image/*'}"/>
Use directiveflowSrc
:
<div*ngFor="let transfer of (flow.transfers$ | async).transfers"><img[flowSrc]="transfer"/></div>
Subscribe toevents$
. NgxFlow listens for these events:filesSubmitted
,fileRemoved
,fileRetry
,fileProgress
,fileSuccess
,fileError
of flow.js. EventfileSubmitted
is fired when user drops or selects a file.
exportclassAppComponentimplementsAfterViewInit,OnDestroy{ @ViewChild('flow')flow:FlowDirective;autoUploadSubscription:Subscription;ngAfterViewInit(){this.autoUploadSubscription=this.flow.events$.subscribe((event)=>{if(event.type==='filesSubmitted'){this.flow.upload();}});}ngOnDestroy(){this.autoUploadSubscription.unsubscribe();}}
npm run build:lib
- builds the library into dist folder
After that you can publish to npm repository fromdist
folder:
cd dist/ngx-flownpm publish
About
flow.js file upload for angular
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors14
Uh oh!
There was an error while loading.Please reload this page.