Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
¿Cómo no creas una secuencia cada vez que cambias la entrada?

Tengo el siguiente código:

 public upload(): void { const fileinput = document.getElementById('file'); fileinput.click(); fileinput.addEventListener('change', this.handleFileInput.bind(this)); } private handleFileInput(event) { try { const input = event.target as HTMLInputElement; const reader = new FileReader(); const { objectId } = this.object; const file = input.files[0]; reader.onload = () => { event.target.value = null; this.objectDetailsService .upload(file, objectId) .subscribe( () => //, () => //, ); }; reader.readAsArrayBuffer(file); } catch (e) { console.log(e); } }

Debería permitir cargar el mismo archivo a veces. Cuando el usuario selecciona otro, este código se crea como una nueva transmisión. ¿Cómo evitarlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tienes razón al preguntar cómo aplicar switchMap . Una forma es crear un observable para manejar la carga de devolución de llamada onload . Luego, podría aplicar operadores como switchMap para llamar a otras funciones asíncronas.

También sugeriría adjuntar el controlador de eventos directamente en la plantilla en lugar de usar document.getElementById('file'); en el controlador.

Prueba lo siguiente

* .ts

 import { Observable, Observer, timer } from 'rxjs'; import { switchMap } from 'rxjs/operators'; public handleFileInput(event) { const input = event.target as HTMLInputElement; const file = input.files[0]; this.readFile(file).pipe( switchMap((file: any) => { const { objectId } = this.object; return this.objectDetailsService.upload(file, objectId); }) ).subscribe({ next: () => console.log('File uploaded'), error: (error: any) => console.log('Error:', error), }); } readFile(file): Observable<any> { const reader = new FileReader(); reader.readAsArrayBuffer(file); return new Observable((observer: Observer<any>) => { reader.onload = (ev: ProgressEvent) => { observer.next(file); observer.complete(); }; reader.onerror = (error: any) => { observer.error(error); }; }); }

* .html

 Select file: <input type="file" (change)="handleFileInput($event)" />

Ejemplo de trabajo: Stackblitz

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!