Reputation: 219
I'm using FileTransfer and FileOpener to download then view a file within an Ionic application, using the native file opener to view the file.
With IOS, when I attempt to download specific files (in this case PDF), I get the error:
{"code": 1, ... "Could not create target file"}
This works with some documents (all the same size) and with others it works fine.
viewFile() {
this.analytics.trackEvent('document', 'view/download', this.document.originalFileName);
let fileName = this.document.originalFileName;
let filePath = `${this.file.dataDirectory}downloads/${fileName}`;
this.fileTransfer.download(this.document.fileUrl, filePath)
.then(file => {
this.openFile(filePath, fileName);
this.analytics.trackEvent('document', 'view/download', this.insightId);
})
.catch(error => {
console.log(`error: ${JSON.stringify(error)}`);
});
this.close();
}
Upvotes: 0
Views: 812
Reputation: 219
The issue I had with this was it wasn't accepting whitespaces.
I had to URI encode and decode the fileName (if it had a whitespace in it).
The following now works:
viewFile() {
this.analytics.trackEvent('document', 'view/download', this.document.originalFileName);
let fileName = encodeURI(this.document.originalFileName);
let filePath = "${this.file.dataDirectory}downloads/${fileName}";
this.fileTransfer.download(this.document.fileUrl, filePath)
.then(file => {
this.openFile(filePath, fileName);
this.analytics.trackEvent('document', 'view/download', this.insightId);
})
.catch(error => {
console.log("error: ${JSON.stringify(error)}");
});
this.close();
}
openFile(filePath, fileName) {
let mimeType = mime.lookup(fileName);
this.fileOpener.open(decodeURI(filePath), mimeType)
.catch(error => {
if (error.status === 9) {
this.messageService.sendMessage(new ErrorMessage("No viewer found for this file type"));
}
console.log("error: ${JSON.stringify(error)}");
});
}
Upvotes: 1