Reputation: 87
I am using ionic-file-trasnfer in my ionic project I use same as documentation like following :-
download() {
const url = 'http://www.example.com/file.pdf';
fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
}
But, I got error code 1.. It goes in error callback function not in success callback
Upvotes: 0
Views: 735
Reputation: 867
You have to create new filetransfer object into this function. Kindly find below code
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
import { FileOpener } from '@ionic-native/file-opener';
constructor(private transfer: FileTransfer, private file: File, private fileOpener: FileOpener) { }
...
download() {
const url = 'http://www.example.com/file.pdf';
const fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.download(url, this.file.dataDirectory + 'file.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));
}, (error) => {
// handle error
});
For opening file you have to add fileOpener plugin (https://ionicframework.com/docs/native/file-opener/), i just updated code kindly review it.
Hope this will help you!
Upvotes: 1
Reputation: 70
import { DocumentViewer} from '@ionic-native/document-viewer';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
import {File} from '@ionic-native/file';
import {Toast} from '@ionic-native/toast';
constructor(private document:DocumentViewer, private file: File, private transfer: FileTransfer, private toast: Toast){}
.....
public download() {
let path = null;
if(this.platform.is('ios')){
path = this.file.documentsDirectory;
}else if(this.platform.is('android')){
path = this.file.dataDirectory;
}
const fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.download('http://www.example.com/file.pdf',path + 'file.pdf').then((entry)=>{
let url = entry.toURL();
this.toast.show('Download Complted', '5000','center').subscribe(
toast=>{
console.log(toast);
}
);
this.document.viewDocument(url, 'application/pdf', {});
});
}
I've edited your code a bit, you can take a look at this code. I hope this will help you.
Upvotes: 0