Issue
I'm working on an Angular Ionic 7 App with capacitor 5. At one page of my app is a list of different files (pdf, txt,csv). The user can select a file and it should download to the device. I tryed to create a Object Url and open it with window.open(fileURL), but nothing happens, also no error. I tryed the library @capacitor/filesystem with downloadFile() Api. The result contins a blob but I'm not able to store it on the device. Does anybody know how to do this?
Filesystem.downloadFile({ path: Directory.Data, url: url, directory: Directory.Data }).then((res) => {
console.log('download finished');
});
Solution
With the help of Raphaƫl Balet's Answer I played around and found a shorter solution. For the downloadFile() api use at least the file name to store the file inside the directory. As url i use an api path.
// download the file to file system or open it in browser
selectFile() {
const url = this.createFileUrl('api/issues/attachement/' + this.messageId + '/' + this.attachment.id);
if (Capacitor.isNativePlatform()) {
Filesystem.downloadFile({
path: this.attachment.attachmentName,
url: url,
directory: Directory.Documents,
}).then((res: DownloadFileResult) => {
this._openFileWithType(res.path, this.attachment.attachmentType);
});
} else {
Browser.open({ url: url });
}
}
// Open the file
private async _openFileWithType(filePath: string, fileType: string) {
const fileOpenerOptions: FileOpenerOptions = {
filePath: filePath,
contentType: fileType,
};
await FileOpener.open(fileOpenerOptions)
.then(() => {
// 'File is opened'
})
.catch((error) => {
console.error(error);
});
}
Answered By - keschra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.