-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploads.component.ts
More file actions
76 lines (60 loc) · 2.12 KB
/
Copy pathuploads.component.ts
File metadata and controls
76 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Component } from '@angular/core';
import { MessageService, PrimeNGConfig } from 'primeng/api';
import { FileUploadModule } from 'primeng/fileupload';
import { ButtonModule } from 'primeng/button';
import { CommonModule } from '@angular/common';
import { BadgeModule } from 'primeng/badge';
import { HttpClientModule } from '@angular/common/http';
import { ProgressBarModule } from 'primeng/progressbar';
import { ToastModule } from 'primeng/toast';
@Component({
selector: 'app-uploads',
standalone: true,
imports: [FileUploadModule, ButtonModule, BadgeModule, ProgressBarModule, ToastModule, HttpClientModule, CommonModule],
providers: [MessageService],
templateUrl: './uploads.component.html',
styleUrl: './uploads.component.scss'
})
export class UploadsComponent {
files = [];
totalSize: number = 0;
totalSizePercent: number = 0;
constructor(private config: PrimeNGConfig, private messageService: MessageService) { }
choose(event: Event, callback: any) {
callback();
}
onRemoveTemplatingFile(event: any, file: File, removeFileCallback: any, index: number) {
removeFileCallback(event, index);
this.totalSize -= parseInt(this.formatSize(file.size));
this.totalSizePercent = this.totalSize / 10;
}
onClearTemplatingUpload(clear: any) {
clear();
this.totalSize = 0;
this.totalSizePercent = 0;
}
onTemplatedUpload() {
this.messageService.add({ severity: 'info', summary: 'Success', detail: 'File Uploaded', life: 3000 });
}
onSelectedFiles(event: any) {
this.files = event.currentFiles;
this.files.forEach((file: any) => {
this.totalSize += parseInt(this.formatSize(file.size));
});
this.totalSizePercent = this.totalSize / 10;
}
uploadEvent(callback: any) {
callback();
}
formatSize(bytes: any) {
const k = 1024;
const dm = 3;
const sizes = this.config.translation.fileSizeTypes || [];
if (bytes === 0) {
return `0 ${sizes[0]}`;
}
const i = Math.floor(Math.log(bytes) / Math.log(k));
const formattedSize = parseFloat((bytes / Math.pow(k, i)).toFixed(dm));
return `${formattedSize} ${sizes[i]}`;
}
}