使用fileOpener Ionic2在app文件夹中打开一个文件 [英] Open a file within the app folder with fileOpener Ionic2

查看:1051
本文介绍了使用fileOpener Ionic2在app文件夹中打开一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FileOpener } from 'ionic-native';

@Component({
  selector: 'page-installHelper',
  templateUrl: 'installHelper.html'
})
export class InstallHelper {

  constructor(public navCtrl: NavController) {
              FileOpener.open('assets/app.apk', 'application/vnd.android.package-archive').then(
      function(){

          console.log("success");

          }, function(err){

              console.log("status : "+err.status);
              console.log("error : "+err.message);

          });
  }

}

但我无法访问文件app.apk在assets / app.apk

but I can't access the file app.apk which is in assets/app.apk

我收到错误:

Status : 9
Error : File Not Found

是不是甚至可以访问app文件夹中的文件?

is it even possible to access a file within the app folders ?

推荐答案

我是通过让应用程序从服务器下载来实现的我在手机中创建的本地文件夹并立即/自动安装,

Well I did it by making the app get downloaded from a server to a local folder I created in the phone and install it immediately/automatically,

以下是其他人有一天需要的代码:

Here is the code in case someone else needed it one day :

import { Component } from '@angular/core';
import { Platform, LoadingController } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { FileOpener } from 'ionic-native';
import { File } from 'ionic-native';
import { Transfer } from 'ionic-native';
import { HomePage } from '../pages/home/home';
declare var cordova: any;


@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
  rootPage = HomePage;

  constructor(platform: Platform, public loadingCtrl: LoadingController) {
      let me = this;
    platform.ready().then(() => {

      let loading = me.loadingCtrl.create({
        content: 'Preparing The App ...'
      });
      loading.present();
      File.createDir(cordova.file.externalDataDirectory, "appFolder", true).then(function(link){ 

          const fileTransfer = new Transfer();
          let url = 'http://yourserverhere.com/app.apk';
          fileTransfer.download(url, cordova.file.externalDataDirectory +"appFolder/app.apk").then((entry) => {
            loading.dismiss();
            FileOpener.open(entry.toURL(), "application/vnd.android.package-archive").then(
              function(){
                 console.log("success");
              },function(err){
                  console.log("status : "+err.status);
                  console.log("error : "+err.message);
              });
          }, (error) => {
            console.log(error);
          });

      },function(error){
          console.log(error);
      });


      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }
}

任何解释只是问我。

这篇关于使用fileOpener Ionic2在app文件夹中打开一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆