I am trying to open a PDF in my ionic application and from my server I am receiving a status code 200 which means it is successfully receiving the data in order to open the PDF.
However, I am receiving this error:
capacitor-runtime.js:81 ERROR Error: Uncaught (in promise): FileTransferError: {“code”:3,“source”:“https://cr-st-web6.almacgroup.com:8725/CSMobileApi/webservices/pdf/169481",“target”:“file:///data/user/0/io.ionic.starter/files/file.pdf”,“http_status”:401,“body”:null,"exception”:null}
at resolvePromise (polyfills.js:3193)
at resolvePromise (polyfills.js:3150)
at polyfills.js:3254
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2785)
at Object.onInvokeTask (vendor.js:53413)
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2784)
at Zone.push…/node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2557)
at drainMicroTaskQueue (polyfills.js:2963)
I have been assured that this is not a problem with our API and is a runtime error but this has been working in the past and Im unsure what this error is telling me.
Here is my function to get and open the pdf:
const loading = await this.loadingController.create({
message: 'Loading ' + msg + ' report...'
});
await loading.present();
// Get Access token from storage
this.storage.get(ACCESS_TOKEN).then(token => {
console.log('token from storage: ', token);
const urlOrderNo = this.apiUrl + pathExtension + ID;
console.log(urlOrderNo);
this.nativeHttp.get(urlOrderNo, {}, { Authorization: 'Bearer ' + token})
.then(data => {
console.log('status: ', data.status);
console.log('data: ', data.data); // data from server
loading.dismiss();
if (data.status === 200) {
// Get mobile platform
if (this.platform.is('android')) {
const uri = encodeURI(urlOrderNo);
const path = this.file.dataDirectory;
const transfer = this.ft.create();
transfer.download(uri, path + 'file.pdf', false,
{headers: { Authorization: 'Bearer ' + token}})
.then ( entry => {
const url = entry.toURL();
loading.dismiss();
console.log('url: ' + url);
this.fileOpener
.open(url, 'application/pdf')
.then(() => console.log('File is opened'))
.catch(e => console.log('Error opening file', e));
})
.then(error => {
console.log(error);
});
} else if (this.platform.is('ios')) {
const uri = encodeURI(urlOrderNo);
const path = this.file.documentsDirectory;
const transfer = this.ft.create();
const setName = Date.now();
transfer.download(uri, path + `${setName}.pdf`, false,
{headers: { Authorization: 'Bearer ' + token}})
.then ( entry => {
const url = entry.toURL();
loading.dismiss();
console.log('url: ' + url);
this.fileOpener
.open(url, 'application/pdf')
.then(() => console.log('File is opened'))
.catch(e => console.log('Error opening file', e));
})
.then(error => {
console.log(error);
});
}