Hello, I have a problem with my code
What I want to do is that when I press the button in my application it downloads a pdf file that is inside the app, but I have not achieved my goal.
So can you help me what am I doing wrong?
import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { AccesoriesService } from '../services/accesories/accesories.service';
import { ProblemaPage } from '../problema/problema.page';
import { File } from '@ionic-native/file/ngx';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-accesorios',
templateUrl: './accesorios.page.html',
styleUrls: ['./accesorios.page.scss'],
})
export class AccesoriosPage implements OnInit {
EstadoAcc: string = 'Problema';
loadAccesory: string = "";
padding: string = "ion-padding ion-activatable ripple-parent rectangle";
toWait: boolean = true;
constructor(
private DataAcc: AccesoriesService,
public modalCtrl: ModalController,
private file: File,
private http: HttpClient
) { }
ngOnInit() {
this.loadAccesory = this.DataAcc.AccVer
}
CheckAcc(event: any) {
this.EstadoAcc = event.detail.value;
}
async GoToProblem(ThatPro: string) {
this.DataAcc.AccList = ThatPro
const modal = await this.modalCtrl.create({
component: ProblemaPage,
});
modal.present();
}
GoBack() {
this.modalCtrl.dismiss();
}
downloadPdf() {
const pdfUrl = 'assets/manuales/SeinmexCosechaArriba.pdf';
this.http.get(pdfUrl, { responseType: 'blob' }).subscribe(response => {
const blob = new Blob([response], { type: 'application/pdf' });
const directory = this.file.externalRootDirectory + 'Download/';
const fileName = 'SeinmexCosechaArriba.pdf';
this.file.writeFile(directory, fileName, blob, { replace: true }).then(() => {
console.log('Archivo descargado correctamente');
}).catch(error => {
console.error('Error al descargar el archivo', error);
});
});
}
}
This puts me on the console:
ERROR TypeError: Cannot read properties of undefined (reading ‘then’)
at Object.next (accesorios.page.ts:59:72)
at ConsumerObserver.next (Subscriber.js:91:33)
at SafeSubscriber._next (Subscriber.js:60:26)
at SafeSubscriber.next (Subscriber.js:31:18)
at map.js:7:24
at OperatorSubscriber._next (OperatorSubscriber.js:13:21)
at OperatorSubscriber.next (Subscriber.js:31:18)
at filter.js:6:128
at OperatorSubscriber._next (OperatorSubscriber.js:13:21)
at OperatorSubscriber.next (Subscriber.js:31:18)