Articles in this section
Category / Section

How to create an Angular 7 Uploader component ?

3 mins read

A quick start project that helps you to create an Angular 7 Uploader component with a minimal code configuration.

Angular 7 Uploader

The following section explains the steps required to create a simple Angular 7 Uploader component.

Pre-requisites

Make sure that you have the compatible versions of Angular in your machine before starting to work on this project.

Introduction

The Angular 7 Uploader used in this project is created from the Syncfusion ej2-angular-inputs package. You can simply define it as <ejs-uploader> within the template.

Dependencies

Before starting with this project, the Angular 7 Uploader requires to add the Syncfusion ej2-angular-inputs package from npmjs, which are distributed in npm as @syncfusion scoped packages.

Creating Angular Project

To create the Angular project using the Angular CLI tool, follow the given steps.

Install Angular CLI 7 using following command.

npm install @angular/cli@7.0.4 -g

 

Note:

If you would like to follow and run the application in Angular6 or Angular5 or Angular4, you need to replace the CLI command version number with the corresponding angular version number.

 

npm install -g @angular/cli@<CLI VERSION>

 

Now, create a new Angular project by using the command ng new and navigate to that folder.

ng new <project name>
cd <project name>

 

Install the ej2-angular-inputs package through the npm install command.

npm install @syncfusion/ej2-angular-inputs --save

 

Adding Angular 7 Uploader

You can add the Angular 7 Uploader component by using ejs-uploader directive and the attributes within the tag allows you to define other functionalities.

Import the Uploader module into the Angular application (app.module.ts) from the ej2-angular-inputs package.

import {BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { UploaderModule } from '@syncfusion/ej2-angular-inputs';
import { AppComponent } from './app.component';
 
@NgModule({ 
    imports: [ BrowserModule, UploaderModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
 
export class AppModule {}

Define the Angular Uploader code within the app.component.html file mapped against the templateUrl option in app.component.ts file.

Here, the Uploader component rendered with the autoUpload property.

[app.component.ts]

import {Component} from '@angular/core';
@Component ({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    constructor () {}
}

 

[app.component.html]

<ejs-uploader id='fileupload' [autoUpload]='false' ></ejs-uploader>

 

Refer to the CDN link of CSS reference within the index.html file.

[index.html]

<link href="https://cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />

Run the application with the command ng serve and have an Uploader with autoUpload will be displayed on the browser as shown below

uploader input

 

uploader list

Asynchronous/Chunk Upload

The Uploader component allows to upload files asynchronously. This Upload process requires save and remove action URL to manage the upload process in the server.

Note:

The save action is necessary to handle the upload operation but the remove action is optional.

The Uploader sends the large file split into small chunks and transmits to the serve using AJAX and you can also pause, resume, and retry the filed chunk file

Note:

The Chunk upload works in asynchronous upload only.

To enable the chunk upload, set the size to chunkSize option of the upload and it receives the value in bytes. RetryAfterDelay and RetryCount is the additional configurations of chunk upload.

Here, uploader component rendered with asynchronous and chunk upload.

[app.component.ts]

import {Component} from '@angular/core';
 
@Component ({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
 
export class AppComponent {
  public path: Object = {
    // provided the wrong url to showcase the chunk upload failure related properties.
    saveUrl: 'https://aspnetmvc.syncfusion.com/services/api/uploader/Save',
    removeUrl: 'https://aspnetmvc.syncfusion.com/services/api/uploader/Remove',
    // set chunk size for enable the chunk upload
    chunkSize: 102400,
    // set count for automatic retry when chunk upload failed
    retryCount: 5,
    // set time delay for automatic retry when chunk upload failed
    retryAfterDelay: 3000
  };
    constructor () {}
}

 

[app.component.html]

<ejs-uploader #defaultupload [asyncSettings]='path' ></ejs-uploader>

Run the application with the command ng serve and have an Uploader with asynchronous and chunk upload displayed on the browser.                              

Also, you can download and run the sample from this GitHub Repository. For more information about Uploader functionalities, refer UG Documentation, API Reference and Samples section.


Conclusion

I hope you enjoyed learning about how to create an Angular 7 File Uploader component.

You can refer to our Angular File Uploader featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied