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

270
Views
Cómo descargar correctamente un archivo de Excel con Angular2

Este es el código de mi servicio que hace que la solicitud de publicación responda a un archivo xls:

 exportInternalOrder(body) { let user_token: string = this.sessionService.getToken(); let headers = new Headers(); headers.append('responseType', 'arraybuffer'); headers.append('Authorization', 'Bearer ' + user_token); return this.http.post(this.config.exportInternalOrder, body,{ headers: headers }).map(res => new Blob([res._body],{ type: 'application/vnd.ms-excel' })); }

Que se supone que maneja la respuesta del archivo de Excel. Este es el código que lo invoca:

 let objToSend = this.makeObjToSend(false); this.reportingService.exportExcel(objToSend) .subscribe( data => { this.exportData(data); }, error => { this.errorFilterMsg.push({ severity: 'error', detail: 'Report exporting has failed!' }); } );

Y este es el guardado del archivo (por alguna razón, window.open no hace nada):

 exportData(data){ let blob = data; let a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = 'fileName.xls'; document.body.appendChild(a); a.click(); }

Pero el archivo aún se guarda como dañado. Mientras usa cartero y curl, viene bien. Cualquier ayuda sería apreciada.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El tipo de responseType no debe establecerse en los headers , es parte del objeto RequestOptionsArgs que se pasa como segundo argumento en la función de post y RequestOptionsArgs contiene headers , tipo de responseType y otros, puede leer más sobre esto aquí . Entonces, su código debería verse así:

 import { ResponseContentType } from '@angular/http'; exportInternalOrder(body) { let user_token: string = this.sessionService.getToken(); let headers = new Headers(); headers.append('Authorization', 'Bearer ' + user_token); return this.http.post(this.config.exportInternalOrder, body,{ headers: headers, responseType: ResponseContentType.Blob }).map(res => new Blob([res._body],{ type: 'application/vnd.ms-excel' })); }
about 4 years ago · Santiago Trujillo Report

0

Estoy escribiendo esto ... será útil para otros que buscan la solución Angular 2 para la funcionalidad de descarga de archivos. El siguiente código funciona para mí.

 import { ResponseContentType } from '@angular/http'; exportInternalOrder(body) { let user_token: string = this.sessionService.getToken(); let headers = new Headers(); headers.append('Authorization', 'Bearer ' + user_token); return this.http.post(this.config.exportInternalOrder, body,{ headers: headers, responseType: ResponseContentType.Blob}).map(res => new Blob([res.blob()],{ type: 'application/vnd.ms-excel' })); }
about 4 years ago · Santiago Trujillo 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!