Search results

Getting started with Angular Uploader component

12 Aug 2022 / 6 minutes to read

This section explains how to create and configure the uploader component in Angular. The uploader component is available in @syncfusion/ej2-angular-inputs package. Utilize this package to render the uploader Component.

Dependencies

The following packages are required to render the uploader component in your Angular application.

Copied to clipboard
|-- @syncfusion/ej2-angular-inputs
    |-- @syncfusion/ej2-angular-base
    |-- @syncfusion/ej2-angular-popups
    |-- @syncfusion/ej2-angular-buttons
    |-- @syncfusion/ej2-inputs
        |-- @syncfusion/ej2-base
        |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons

Setup angular environment

Angular provides the easiest way to set angular CLI projects using Angular CLI tool.

Install the CLI application globally to your machine.

Copied to clipboard
npm install -g @angular/cli

Create a new application

Copied to clipboard
ng new syncfusion-angular-uploader

By default, it install the CSS style base application. To setup with SCSS, pass —style=scss argument on create project.

Example code snippet.

Copied to clipboard
ng new syncfusion-angular-uploader --style=scss

Navigate to the created project folder.

Copied to clipboard
cd syncfusion-angular-uploader

Installing Syncfusion Uploader Package

Syncfusion packages are distributed in npm as @syncfusion scoped packages. You can get all the Angular Syncfusion package from npm link.

Currently, Syncfusion provides two types of package structures for Angular components,

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.

Ivy library distribution package

Syncfusion Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-inputs package to the application.

Copied to clipboard
npm install @syncfusion/ej2-angular-inputs --save

Angular compatibility compiled package(ngcc)

For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc package use the below.

Add @syncfusion/ej2-angular-inputs@ngcc package to the application.

Copied to clipboard
npm install @syncfusion/ej2-angular-inputs@ngcc --save

To mention the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

Copied to clipboard
@syncfusion/ej2-angular-inputs:"20.2.38-ngcc"

Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.

Initialize Uploader

The below couple of steps describes to initialize the uploader component in Angular application.

  • Import UploaderModule from @syncfusion/ej2-angular-inputs package, inject into imports section of NgModule in the module file src/app/app.module.ts to use the Uploader component across the application.
Copied to clipboard
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { UploaderModule  } from '@syncfusion/ej2-angular-inputs';
@NgModule({
  imports:      [ BrowserModule , UploaderModule],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Adding CSS reference

The following CSS files are available in ../node_modules/@syncfusion package folder. This can be referenced in [src/styles.css] using following code.

Copied to clipboard
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-inputs/styles/material.css';

The Custom Resource Generator (CRG) is an online web tool, which can be used to generate the custom script and styles for a set of specific components. This web tool is useful to combine the required component scripts and styles in a single file.

Adding Uploader component

Modify the template in [src/app/app.component.ts] file to render the Uploader component. Add the Angular Uploader by using <ejs-uploader> selector in template section of the app.component.ts file.

Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
           <ejs-uploader #defaultupload  [autoUpload]='false'></ejs-uploader>
          `
})
export class AppComponent {
constructor() {
}
}

Running the application

After completing the configuration required to render the uploader, run the application using the following command to display the output in your default browser.

Copied to clipboard
ng serve

From v16.2.41 version, the Essential JS2 AJAX library has been integrated for uploader server requests. Hence, use the third party promise library like blue-bird to use the uploader in Internet Explorer.

The below example shows a uploader component.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
           <ejs-uploader #defaultupload  [autoUpload]='false'></ejs-uploader>
          `
})
export class AppComponent {
constructor() {
}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UploaderModule } from '@syncfusion/ej2-angular-inputs';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, UploaderModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {
}
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Adding drop area

By default, the uploader component allows to upload files by drag the files from file explorer, and drop into the drop area. You can configure any other external element as drop target using dropArea property.

In the following sample, external element is configured as drop target to uploader component.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
           <div id='droparea'>
        Drop files here to upload
    </div>
    <div id='uploadfile' >
           <ejs-uploader #defaultupload  [autoUpload]='false' [dropArea]='dropEle'></ejs-uploader>
           </div>
          `
})
export class AppComponent {
public dropEle: HTMLElement ;
ngOnInit() {
      this.dropEle = document.getElementById('droparea');
}
constructor() {
}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UploaderModule } from '@syncfusion/ej2-angular-inputs';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, UploaderModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {
}
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Configure asynchronous settings

By default, the uploader component process the files in Asynchronous mode. Define the properties saveUrl and removeUrl to handle the save and remove action as follows.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
           <div id='droparea'>
        Drop files here to upload
    </div>
    <div id='uploadfile' >
           <ejs-uploader #defaultupload  [autoUpload]='false' [dropArea]='dropEle' [asyncSettings]='path'></ejs-uploader>
           </div>
          `
})
export class AppComponent {
 public path: Object = {
  saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save',
  removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove' };
constructor() {
}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UploaderModule } from '@syncfusion/ej2-angular-inputs';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, UploaderModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {
}
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Handle success and failed upload

You can handle the success and failure actions using the success and failure events. To handle these events, define the function and assign it to corresponding event as follows.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
           <ejs-uploader #defaultupload [autoUpload]='false' [dropArea]='dropEle' [asyncSettings]='path' (success)="onUploadSuccess($event)" (failure)="onUploadFailure($event)"></ejs-uploader>
          `
})
export class AppComponent {
public path: Object = {
  saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save',
  removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove' };
  public onUploadSuccess(args: any): void  {
    if (args.operation === 'upload') {
        console.log('File uploaded successfully');
    }
}

public onUploadFailure(args: any): void  {
console.log('File failed to upload');
}

public dropEle: HTMLElement ;
ngOnInit() {
      this.dropEle = document.getElementById('droparea');
}

constructor() {
}
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UploaderModule } from '@syncfusion/ej2-angular-inputs';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, UploaderModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule {
}
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

You can also explore Angular File Upload feature tour page for its groundbreaking features. You can also explore our Angular File Upload example to understand how to browse the files which you want to upload to the server.

See Also

N> You can also explore Angular File Upload feature tour page for its groundbreaking features. You can also explore our Angular File Upload example to understand how to browse the files which you want to upload to the server.