Project

General

Profile

1
import { FILE_TYPE } from './../enums/FILE_TYPE.enum';
2
import { Injectable } from '@angular/core';
3

    
4
@Injectable({
5
  providedIn: 'root'
6
})
7
export class UtilsService {
8

    
9
  constructor() { }
10

    
11
  public base64ToFile(base64Data: string, contentType: FILE_TYPE): Blob {
12
    const byteString = window.atob(base64Data);
13
    const arrayBuffer = new ArrayBuffer(byteString.length);
14
    const int8Array = new Uint8Array(arrayBuffer);
15
    for (let i = 0; i < byteString.length; i++) {
16
      int8Array[i] = byteString.charCodeAt(i);
17
    }
18
    const blob = new Blob([int8Array], { type: this.convertContentType(contentType) });
19
    return blob;
20
  }
21

    
22
  public convertContentType(type: FILE_TYPE): string {
23
    switch (type) {
24
      case FILE_TYPE.DOC:
25
        return 'application/msword';
26
      case FILE_TYPE.DOCX:
27
        return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
28
      case FILE_TYPE.PDF:
29
        return 'application/pdf';
30
      case FILE_TYPE.PNG:
31
        return 'image/png';
32
      case FILE_TYPE.XLS:
33
        return 'application/vnd.ms-excel';
34
      case FILE_TYPE.XLSX:
35
        return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
36
    }
37
  }
38
}
(2-2/2)