Ionic 2 - (窗口为任意).resolveLocalFileSystemURL()抛出错误

时间:2017-06-09 23:54:26

标签: javascript file typescript ionic-framework window

我有一些使用相机的打字稿代码:

this.camera.getPicture(this.optionsGetCamera).then((imageURI) => {
    if (this.platform.is('ios')) {
        this.crop.crop(imageURI, { quality: 100 }).then(newPath => {
            console.log(newPath);
            (window as any).resolveLocalFileSystemURL(newPath, function (fileEntry) {
                 this.readFile(fileEntry);
            });
        }).catch((error) => { ...

当我运行应用程序并使用在此代码中使用的相机功能时,我在控制台中收到错误(我在使用-lc参数的设备上运行以查看日志):

console.error: ERROR -> {"line":58535,"column":81,"sourceURL":"http://192.168.1.131:8100/build/main.js","__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

line 58535就是这样:

window.resolveLocalFileSystemURL(newPath, function (fileEntry) {

这一行位于main.js文件中,该文件由ionic on build创建。我一直试图理解"source":"Promise.then",因为它似乎可能指向问题(有承诺?还是没有使用?)。感谢。

2 个答案:

答案 0 :(得分:1)

使用离子本机的文件类意味着Typescript类 不要使用javascript 任何类型 参考https://ionicframework.com/docs/native/file/

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

...

File.listDir(cordova.file.applicationDirectory, 
'mySubFolder/mySubSubFolder').then(
  (files) => {
    // do something
  }
).catch(
  (err) => {
    // do something
  }
);

答案 1 :(得分:0)

cordova plugin add cordova-plugin-file为我工作了我的代码

`getMedia(): Promise<any> {
    return new Promise((resolve, reject) => {
      Camera.getPicture(this.options).then((fileUri: any) => {
        alert('File URI: ' + JSON.stringify(fileUri));
        window.resolveLocalFileSystemURL('file://' + fileUri, (fileEntry) => {
          alert('Type: ' + (typeof fileEntry));
          fileEntry.file((file) => {
            alert('File: ' + (typeof file) + ', ' + JSON.stringify(file));
            const fileReader = new FileReader();
            fileReader.onloadend = (result: any) => {
              alert('File Reader Result: ' + JSON.stringify(result));
              let arrayBuffer = result.target.result;
              let blob = new Blob([new Uint8Array(arrayBuffer)], { type: 'video/mp4' });
              const name = 'mypic'
              this.upload(blob);
            };
            fileReader.onerror = (error: any) => {
              reject(error);
            };
            fileReader.readAsArrayBuffer(file);
          }, (error) => {
            alert('File Entry Error: ' + JSON.stringify(error));
          });
        }, (error) => {
          alert('Error resolving file: ' + JSON.stringify(error));
        });
      });
    });
  }`