Filewriter.write()不起作用Ionic 2

时间:2017-04-28 11:19:56

标签: android ios ionic2 blob cordova-plugin-file

我正在尝试使用cordova在我的应用上开发服务,以便在设备(iOS和Android)的存储上下载和写入文件。代码不会创建新文件,也不会显示任何错误,这就是文件编写器不能继续执行的原因。

代码有什么问题?找wally plz!

downloadFile(url, fileName) {
    return new Promise((resolve, reject) => {
        let blob
        this.http
            .get(url, { responseType: ResponseContentType.Blob })
            .map(res => res.blob())
            .subscribe(
            data => {
                blob = data;
                if (blob) {
                    console.log('[ViewerService]: blob data recived from ' + url);
                    this.writeFile(blob, fileName).then(
                        fileEntry => {
                            resolve(fileEntry)
                        },
                        error => {
                            reject()
                        }
                    )
                } else {
                    console.error('[ViewerService]: blob data is null');
                    reject();
                }
            })
    })
}


writeFile(blob, fileName) {
    return new Promise((resolve, reject) => {
        this.resolveDir(blob, fileName)
            .then(
            fs => {
                this.createFile(fs, fileName, blob, false)
                    .then(
                    fileEntry => {
                        this.runFileWriter(fileEntry, blob)
                            .then(
                            fileEntry => resolve(fileEntry),
                            e => reject()
                            )
                    },
                    e => console.log(e)
                    )
            },
            e => console.log(e)
            )
    })
}
resolveDir(blob, fileName) {
    return new Promise((resolve, reject) => {
        console.log('[FileService]: Resolving URL...');
        this.file.resolveDirectoryUrl(cordova.file.cacheDirectory).then(
            dir => resolve(dir),
            e => reject(e)
        )
    })
}

createFile(dirEntry, fileName, blob, isAppend) {
    return new Promise((resolve, reject) => {
        dirEntry.getFile(fileName, { create: true, exclusive: false },
            function (fileEntry) {
                resolve(fileEntry);
            },
            function (e) {
                reject(e)
            });
    })
}

runFileWriter(fileEntry, dataObj) {
    return new Promise((resolve, reject) => {
        fileEntry.createWriter(function (fileWriter) {
            fileWriter.onwriteend = function () {
                console.log("[FileService]: Successful file write");
                resolve(fileEntry)
            };
            fileWriter.onerror = function (e) {
                console.error("[FileService]: Failed file write: " + e.toString());
                reject()
            };
            if (!dataObj) {
                dataObj = new Blob(['File download error'], { type: 'text/plain' });
            }
            fileWriter.write(dataObj);
        });
    })
}

这是我的package.json

 {
  "name": "testapp",
  "author": "me",
  "homepage": "http://me.com/",
  "private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve",
    "ionic:watch": "ionic-app-scripts watch"
  },
  "dependencies": {
    "@angular/common": "2.2.1",
    "@angular/compiler": "2.2.1",
    "@angular/compiler-cli": "2.2.1",
    "@angular/core": "2.2.1",
    "@angular/forms": "2.2.1",
    "@angular/http": "2.2.1",
    "@angular/platform-browser": "2.2.1",
    "@angular/platform-browser-dynamic": "2.2.1",
    "@angular/platform-server": "2.2.1",
    "@ionic-native/camera": "^3.6.1",
    "@ionic-native/core": "^3.2.1",
    "@ionic-native/file": "^3.6.1",
    "@ionic-native/file-opener": "^3.6.1",
    "@ionic-native/image-picker": "^3.4.4",
    "@ionic-native/network": "^3.2.1",
    "@ionic/storage": "^1.1.7",
    "@ngx-translate/core": "^6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "@types/cordova": "0.0.34",
    "@types/cordova-plugin-device": "0.0.3",
    "@types/cordova.plugins.diagnostic": "^3.4.0",
    "angular2-moment": "^1.2.0",
    "cordova": "^6.5.0",
    "ionic-angular": "2.0.0-rc.5",
    "ionic-native": "^2.9.0",
    "ionicons": "3.0.0",
    "jquery": "^3.1.1",
    "ng2-charts": "^1.5.0",
    "ng2-translate": "^5.0.0"
  },
  "devDependencies": {
    "@ionic-native/diagnostic": "^3.4.3",
    "@ionic/app-scripts": "^1.1.4",
    "angular-translate-loader-static-files": "^2.15.1",
    "typescript": "2.0.9"
  },
  "cordovaPlugins": [
    "cordova-plugin-device@1.1.5",
    "cordova-plugin-splashscreen@4.0.2",
    "cordova-plugin-network-information@1.3.2",
    "cordova-plugin-camera@2.4.0",
    "ionic-plugin-keyboard@~2.2.1",
    "cordova-plugin-whitelist@1.3.1",
    "cordova-plugin-console@1.0.5",
    "cordova-plugin-statusbar@2.2.1",
    "cordova-sqlite-storage@~2.0.2",
    "cordova-plugin-file",
    "cordova-plugin-inappbrowser@~1.7.0",
    "cordova-plugin-file-opener2",
    "cordova-plugin-image-picker@~1.1.1"
  ],
  "cordovaPlatforms": [],
  "description": "testapp"
}

0 个答案:

没有答案