Filestack for Angular

Overview

The @filestack/angular library is a component-based wrapper for the core filestack-js SDK. It’s designed to help you integrate Filestack’s file handling services into your Angular applications with minimal effort and a familiar developer experience. You can implement the powerful Filestack Picker and its features in just a few lines of code. The FilestackAngularModule provides everything you need to get started, including:

  • FilestackService: A wrapper for the filestack-js client with added support for Observables.
  • FilestackTransformPipe: An Angular pipe for easily creating transformation URLs in your templates.
  • PickerOverlayComponent: A component for the Filestack picker in overlay mode.
  • PickerInlineComponent: A component for the picker in inline mode.
  • PickerDropPaneComponent: A component for the picker in drop pane mode.

Usage

Installation

Install the necessary packages from NPM.
npm install filestack-js @filestack/angular

Module Setup

Import FilestackModule into your app.module.ts and configure it with your API key.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FilestackModule } from '@filestack/angular';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    // Add your Filestack API key
    FilestackModule.forRoot({ apikey: 'YOUR_APIKEY' })
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Picker Components

The library provides three main components to render the picker in different ways. They share the following inputs and outputs.

Available Inputs

Name Type Description
apikey String Your Filestack API key.
options Object Controls the picker’s behavior. See PickerOptions.
clientOptions Object Options to initialize the client (cname, security, etc). See ClientOptions.
source String Filestack handle or external URL for actions like transform, remove, or preview.

Available Outputs

Name Type Description
uploadSuccess Subject An event emitter that fires on a successful upload.
uploadError Subject An event emitter that fires when an upload error occurs.

FilestackService

For advanced use cases, you can access the underlying filestack-js client functionality through the FilestackService. This allows you to use methods like transform(), storeURL(), and upload(), while leveraging the power of Observables.

Method Return Description
init() void Initializes the filestack client with your API key.
picker() PickerInstance Opens or closes the picker instance.
transform() string Creates a transformation URL.
retrieve() Observable Retrieves info about a Filestack handle.
metadata() Observable Accesses metadata for a file via its handle.
storeURL() Observable Gets info about a Filestack handle’s metadata.
upload() Observable Uploads a file to Filestack.
remove() Observable Removes a file from storage.

Examples

Open Picker with Custom Button

Use the <ng-picker-overlay> component and place a custom button inside it to act as the trigger.

<!-- component.html -->
<ng-picker-overlay
  [apikey]="apikey"
  (uploadSuccess)="onUploadSuccess($event)">
  <button class="your-custom-button">Open Picker</button>
</ng-picker-overlay>

Render Picker Inline

Use the <ng-picker-inline> component to render the picker directly inside a container in your application’s DOM.

<!-- component.html -->
<ng-picker-inline
  [apikey]="apikey"
  (uploadSuccess)="onUploadSuccess($event)">
</ng-picker-inline>

Use the Transform Pipe

Apply transformations directly in your template using the filestackTransform pipe.

<!-- component.html -->
<img [src]="'FILE_HANDLE' | filestackTransform: transformOptions">

// component.ts
import { TransformOptions } from 'filestack-js';

export class AppComponent {
  transformOptions: TransformOptions = {
    resize: { width: 400 },
    sepia: { tone: 80 }
  };
}

Upload with FilestackService

Use a standard file input and the service to programmatically upload a file.
<!-- component.html -->
<input type='file' (change)="fileChanged($event)">
<button (click)="uploadFile()">Upload File</button>

// component.ts
export class AppComponent implements OnInit {
  file: any;

  constructor(private filestackService: FilestackService) {}

  ngOnInit() {
    this.filestackService.init('YOUR_API_KEY');
  }

  fileChanged(e) {
    this.file = e.target.files[0];
  }

  uploadFile() {
    this.filestackService.upload(this.file)
      .subscribe(res => console.log(res));
  }
}

More Resources

  • CDN Access: The compiled module is on our CDN at https://static.filestackapi.com/filestack-angular/{VERSION}/filestack-angular.umd.min.js.
  • Development: To run the example app locally, run ng serve example and visit http://localhost:4200/.
  • Contribution: Contributions and ideas are welcome! The project follows the conventional commits specification.