Reputation: 319
want to download a video in ionic 2 project which is placed on the server, also what other plugin need to install for this. Need help.
Upvotes: 1
Views: 1729
Reputation: 319
import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';
declare let cordova: any;
@Component({
selector: 'page-about',
templateUrl: 'about.html',
providers: [Transfer, TransferObject, File]
})
export class AboutPage {
storageDirectory: string = '';
constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController) {
this.platform.ready().then(() => {
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.documentsDirectory;
}
else if(this.platform.is('android')) {
file.createDir(file.externalDataDirectory, 'newDir', true).then((result)=>{
console.log("Directory created"+result);
});
this.storageDirectory = cordova.file.externalDataDirectory+ 'newDir/';
console.log(this.storageDirectory);
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
}
download() {
this.platform.ready().then(() => {
/*file.checkDir(file.externalDataDirectory, 'newDir').then( => console.log('Directory exists')).catch(err =>
console.log('Directory doesnt exist'));
console.log('new Directory is created');
*/
});
const fileTransfer: TransferObject = this.transfer.create();
const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';
fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `was not downloaded. Error code: ${error}`,
buttons: ['Ok']
});
alertFailure.present();
});
}
}
Here I got the solution.
Upvotes: 1