Created
March 29, 2019 10:30
-
-
Save ovpv/c0d3e817f6a527a849f0a310fb965932 to your computer and use it in GitHub Desktop.
Service in ionic 4 to provide method for downloading and opening a pdf file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { File } from "@ionic-native/file/ngx"; | |
import { FileTransfer } from "@ionic-native/file-transfer/ngx"; | |
import { FileOpener } from "@ionic-native/file-opener/ngx"; | |
import { Injectable } from "@angular/core"; | |
@Injectable({ | |
providedIn: "root" | |
}) | |
export class PdfViewerService { | |
fileTransfer: FileTransferObject; | |
constructor( | |
private fileOpener: FileOpener, | |
private transfer: FileTransfer, | |
private file: File | |
) {} | |
download(url: string, title: string) { | |
this.fileTransfer = this.transfer.create(); | |
this.fileTransfer | |
.download(url, this.file.dataDirectory + title + ".pdf") | |
.then(entry => { | |
console.log("download complete: " + entry.toURL()); | |
this.fileOpener | |
.open(entry.toURL(), "application/pdf") | |
.then(() => console.log("File is opened")) | |
.catch(e => console.log("Error opening file", e)); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment