import { Component, EventEmitter, Input, Output } from '@angular/core'; import { FileUpload } from '../../interface/file-upload.interface'; import { CircleCheckBig, Download, X } from 'lucide-angular'; @Component({ selector: 'kit-upload-files-file', templateUrl: './upload-files-file.component.html', styleUrls: ['./upload-files-file.scss'] }) export class UploadFilesFileComponent{ iconCircle = CircleCheckBig; iconX = X; iconDownload = Download; @Output() public removeFile: EventEmitter = new EventEmitter(); @Output() public validFile: EventEmitter = new EventEmitter(); public progressBar:number = 0; private _fileUpload!: FileUpload; constructor(){} @Input() public set fileUpload(file: FileUpload){ this._fileUpload = file; this.uploadFile() } public get fileUpload(){ return this._fileUpload } public onRemoveFile(){ this.removeFile.emit() } public getFileSize(size:number){ return `${(size / 1024).toFixed(2)} KB`; } public uploadFile(): void { const reader = new FileReader(); reader.onloadstart = () => { this.progressBar = 0; }; reader.onprogress = (event) => { if (event.lengthComputable) { this.progressBar = Math.round((event.loaded / event.total) * 100); } }; reader.onload = () => { this.validFile.emit(); this.progressBar = 100; }; reader.onerror = () => { console.error('File upload failed'); }; reader.readAsArrayBuffer(this.fileUpload.file); } public downloadFile(): void { const file = this.fileUpload.file; // Verificamos que el archivo existe if (!file) { console.error('No hay un archivo disponible para descargar'); return; } const fileURL = URL.createObjectURL(file); const downloadLink = document.createElement('a'); downloadLink.href = fileURL; downloadLink.download = file.name; document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); setTimeout(() => { URL.revokeObjectURL(fileURL); }, 100); } }