Skip to main content

How to Download Multiple Files as a .Zip File using Angular

This article helps you to download multiple files as a zip file using Angular and SharePoint Online.

Prerequisites

  1. Download and Install Node from Node
  2. Install and create an Angular project using Angular CLI from here: Angular CLI 
  3. Create a document library and upload the sample files in it.

This is image title

To create the zip file in Angular, the way we were required to for the js zip library file.

npm i jszip  

To save the file on the client-side, we are required to file-save the library file.

npm i file-saver 

Create a component(FileDownloaderClient) in Angular for downloading the file on the client side.

 g c File/FileDownloaderClient  

To show the files in the browser, PrimeNG table has been used. To know more details about PrimeNG, refer here.

Now the file-downloader-client.component.html will look like the following:

<p-table [value]="filesArray">  
    <ng-template pTemplate="header">  
        <tr>  
            <th>Sr.No</th>  
            <th>  
                File Name  
            </th>  
        </tr>  
    </ng-template>  
    <ng-template pTemplate="body" let-item let-rowIndex="rowIndex">  
        <tr>  
            <td>{{rowIndex + 1}}</td>  
            <td>  
                <a href={{item.FileRef}}>{{item.FileLeafRef}}</a>  
            </td>  
        </tr>  
    </ng-template>  
</p-table>  
<p-button label="Download" type="button" (click)="downloadFile()"></p-button>  

Import the install library in file-downloader-client.component.ts

import * as JSZip from 'jszip';  
import * as FileSaver from 'file-saver';  

The complete file-downloader-client.component.ts will look like:

import { Component, OnInit } from '@angular/core';  
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';  
import * as JSZip from 'jszip';  
import * as FileSaver from 'file-saver';  
import { TableModule } from 'primeng/table';  
import { GlobalServicesService } from 'src/app/services/global-services.service';  
@Component({  
  selector: 'app-file-downloader-client',  
  templateUrl: './file-downloader-client.component.html',  
  styleUrls: ['./file-downloader-client.component.css']  
})  
export class FileDownloaderClientComponent implements OnInit {  
  jsonHeader = 'application/json; odata=verbose';  
  headersOld = new Headers({ 'Content-Type': this.jsonHeader, Accept: this.jsonHeader });  
  headers = { 'Content-Type': this.jsonHeader, Accept: this.jsonHeader };  
  filesArray: [];  
  showFileArray: [];  
  constructor(  
    private httpClient: HttpClient,  
    private globalService: GlobalServicesService  
  ) { }  
  ngOnInit() {  
    this.getFilesFromLibrary();  
  }  
  async readFiles(listName: string, options?: any) {  
    let res;  
    const url = this.globalService.sharePointPageObject.webAbsoluteUrl + '/_api/web/lists/GetByTitle(\'' + listName + '\')/items?$select=FileRef,FileLeafRef';  
    res = await this.httpClient.get(url, this.getHeaders(true, true)).toPromise().catch((err: HttpErrorResponse) => {  
      const error = err.error;  
      return error;  
    });  
    return this.parseResults(res);  
  }  
  parseResults(res) {  
    if (res) {  
      if (res.hasOwnProperty('d') && res.d.hasOwnProperty('results')) {  
        return res.d.results;  
      } else if (res.hasOwnProperty('error')) {  
        const obj: any = res.error;  
        obj.hasError = true;  
        return obj;  
      } else {  
        return {  
          hasError: true,  
          comments: res  
        };  
      }  
    } else {  
      return {  
        hasError: true,  
        comments: 'Check the response in network trace'  
      };  
    }  
  }  
  getHeaders(bAddContext, returnOp) {  
    const headerCopy: any = Object.assign({}, this.headers);  
    if (bAddContext) {  
      const context: any = document.getElementById('__REQUESTDIGEST');  
      if (context) {  
        headerCopy['X-RequestDigest'] = context.value;  
      }  
    }  
    if (returnOp) {  
      const httpOptions = {  
        headers: new HttpHeaders(headerCopy)  
      };  
      return httpOptions;  
    } else {  
      return headerCopy;  
    }  

  }  

  async getFilesFromLibrary() {  
    const results = await this.readFiles('Test_ABC');  
    this.filesArray = results;  
    console.log(results);  
  }  
  downloadFile() {  
    this.createZip(this.filesArray.map(c => c.FileRef), 'Sample');  
  }  
  async getFile(url: string) {  
    const httpOptions = {  
      responseType: 'blob' as 'json'  
    };  
    const res = await this.httpClient.get(url, httpOptions).toPromise().catch((err: HttpErrorResponse) => {  
      const error = err.error;  
      return error;  
    });  
    return res;  
  }  
  async createZip(files: any[], zipName: string) {  
    const zip = new JSZip();  
    const name = zipName + '.zip';  
    // tslint:disable-next-line:prefer-for-of  
    for (let counter = 0; counter < files.length; counter++) {  
      const element = files[counter];  
      const fileData: any = await this.getFile(element);  
      const b: any = new Blob([fileData], { type: '' + fileData.type + '' });  
      zip.file(element.substring(element.lastIndexOf('/') + 1), b);  
    }  
    zip.generateAsync({ type: 'blob' }).then((content) => {  
      if (content) {  
        FileSaver.saveAs(content, name);  
      }  
    });  
  }  
}  

Now run npm run start in terminal and browse http://localhost:4200/#/fileDownload to view the file on the browser.

Click on the download button to download the file as a zip folder.

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...