Advertisement
superserio

upload-file.component.ts

Aug 24th, 2021
1,706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ChangeDetectorRef, Component, Input, NgZone, OnInit } from '@angular/core';
  2. import { ToastrService } from 'ngx-toastr';
  3.  
  4. import { Observable } from 'rxjs';
  5. import { UploadFileService } from '../../../core/services/upload-service.service';
  6.  
  7. @Component({
  8.   selector: 'kt-upload-file',
  9.   templateUrl: './upload-file.component.html',
  10.   styleUrls: ['./upload-file.component.scss']
  11. })
  12.  
  13. export class UploadFilesComponent implements OnInit {
  14.  
  15.   selectedFiles: FileList;
  16.   progressInfos: any[] = [];
  17.   message: string = '';
  18.   fileCaricati: number = 0;
  19.  
  20.   @Input()
  21.   carica: boolean = true;
  22.  
  23.   fileInfos: Observable<any>;
  24.  
  25.   constructor(private uploadService: UploadFileService,
  26.               private zone: NgZone,
  27.               private cdr: ChangeDetectorRef,
  28.               private toastService: ToastrService) { }
  29.  
  30.   ngOnInit(): void {
  31.     this.fileInfos = this.uploadService.getFiles();
  32.   }
  33.  
  34.   selectFiles(event): void {
  35.     this.progressInfos = [];
  36.     this.selectedFiles = event;
  37.     this.carica = true;
  38.     this.uploadFiles();
  39.   }
  40.  
  41.   uploadFiles(): void {
  42.     if (this.selectedFiles) {
  43.       for (let i = 0; i < this.selectedFiles.length; i++) {
  44.         this.upload(i, this.selectedFiles[i]);
  45.       }
  46.     }
  47.   }
  48.  
  49.   upload(idx: number, file: File): void {
  50.     this.progressInfos[idx] = { value: 0, fileName: file.name };
  51.  
  52.     if (file) {
  53.       if(file.size > 26214400) {
  54.         this.message = 'File troppo grande, la dimensione massima consentita è di 25MB';
  55.         this.progressInfos[idx].value = 0;
  56.         this.toastService.warning(this.message);
  57.       }
  58.       else {
  59.         this.isThisAFile(file).then(_ => {
  60.           this.uploadService.upload(file).subscribe(
  61.             _ => {
  62.               this.zone.run(() => {
  63.                   this.cdr.detectChanges();
  64.                   this.progressInfos[idx].value = this.uploadService.progress;
  65.               });
  66.             },
  67.             _ => {
  68.               this.message = 'Errore nel caricamento del file ' + file.name;
  69.               this.progressInfos[idx].value = 0;
  70.               this.toastService.warning(this.message);
  71.             }, () => {
  72.               this.toastService.success("File " + file.name + " caricato con successo!");
  73.           });
  74.         }).catch(_ => {
  75.           this.message = 'File non valido (non è possibile caricare cartelle)';
  76.           this.progressInfos[idx].value = 0;
  77.           this.toastService.warning(this.message);
  78.         });
  79.       }
  80.     }
  81.   }
  82.  
  83.   // https://stackoverflow.com/questions/52667995/how-to-check-if-selected-file-is-a-directory-or-regular-file
  84.   isThisAFile(file) {
  85.     return new Promise(function (resolve, reject) {
  86.       if (file.type !== '') {
  87.         return resolve(file)
  88.       }
  89.       const reader = new FileReader()
  90.       reader.onloadend = () => {
  91.         if (reader.error && reader.error.name === 'NotFoundError') {
  92.           return reject(reader.error.name)
  93.         }
  94.         resolve(file)
  95.       }
  96.       reader.readAsBinaryString(file)
  97.     })
  98.   }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement