如何将 Ionic 2 中的视频从服务器下载到移动文件系统



想要下载放置在服务器上的 ionic 2 项目中的视频,还需要为此安装哪些其他插件。需要帮助。

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();
      });
    
}
  }

在这里,我得到了解决方案。

最新更新