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

738
Views
Descargar un archivo json (desde json-server) como un archivo txt o json o csv, en una aplicación Angular

Instalé el json-server en mi aplicación Angular que me permite guardar un formulario (llenado por el usuario) a través de una API json falsa en un archivo json:

 { "extracts": [ { "contentName": "xxx", "contentType": "xxx", "contentToStore": "xxx", "id": 1 } ] }

También puedo ver los resultados en mi http://localhost:3000/extracts

En mi plantilla html , mi formulario se envía:

 <form #postForm="ngForm" (ngSubmit)="onSubmitExtractions(postForm.value)">

En el componente correspondiente, he escrito este código:

 onSubmitExtractions(postData: Post) { this .dbService .submitExtractions(postData) .subscribe( postData => { this.jsonPosts.push(postData); } ); this .dbService .downloadExtractions(postData); }

El primer fragmento escribe en el archivo json, con este código en la secuencia de comandos del servicio:

 submitExtractions(post: Post): Observable<Post> { return this.httpClient.post<Post>(this.apiUrl, post, httpOptions); }

Esto funciona bien y obtengo los resultados en un archivo json (base de datos del servidor json falso) y se supone que el segundo fragmento debe descargar este archivo cuyo código en el script de servicio es este:

 downloadExtractions(post: Post) { const blob = new Blob([JSON.stringify(post, null, 2)], { type: 'application/json' }); if (blob) { this.downloaded = 'The blob contains ' + blob.size + ' byte!'; } else { this.downloaded = 'The download failed!'; } }

¿Qué me falta en este código para descargar el contenido json? ¿Cómo descargo este contenido como csv o json o incluso como archivo de texto ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Pruebe esta solución. Está usando el mismo blob solo con algunos datos constantes.

en html

 <a [href]="fileUrl" download="file.txt">DownloadFile</a>

en componente.ts

 export class AppComponent implements OnInit { name = 'Angular 5'; fileUrl; constructor(private sanitizer: DomSanitizer) { } ngOnInit() { const data = 'some text'; const blob = new Blob([data], { type: 'application/octet-stream' }); this.fileUrl = this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob)); } }

Para referencia, por favor visite:

https://stackblitz.com/edit/angular-blob-file-download-text-file?file=app%2Fapp.component.ts

over 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!